Does the display name of the model not display?

I have a model class similar to the following:

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; [System.Runtime.Serialization.DataContract(IsReference = true)] [System.ComponentModel.DataAnnotations.ScaffoldTable(true)] public class TestModel { [Display(Name="Schedule Name")] [Required] public string scheduleName; } 

And in my .cshtml file I have:

  <li> @Html.LabelFor(m => m.scheduleName) @Html.TextBoxFor(m => m.scheduleName, Model.scheduleName) @Html.ValidationMessageFor(m => m.scheduleName) </li> 

But for some reason, my display name is not displayed (the label shows "scheduleName")

I swear I have the same code in other classes, and it seems very good. Can someone please indicate the reason why this will not work?

+6
source share
1 answer

DataAnnotationsModelMetadataProvider works on properties. Your scheduleName should be a property not just as a field.

 [System.Runtime.Serialization.DataContract(IsReference = true)] [System.ComponentModel.DataAnnotations.ScaffoldTable(true)] public class TestModel { [Display(Name="Schedule Name")] [Required] public string scheduleName { get; set; } } 

Note. Under C # naming conventions, your property names must be PascalCased.

+7
source

Source: https://habr.com/ru/post/918579/


All Articles