我有一个类似于以下的模型类:
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; }
在我的.cshtml文件中,我有:
<li> @Html.LabelFor(m => m.scheduleName) @Html.TextBoxFor(m => m.scheduleName,Model.scheduleName) @Html.ValidationMessageFor(m => m.scheduleName) </li>
解决方法
DataAnnotationsModelMetadataProvider适用于属性.您的scheduleName应该是一个不仅仅是“字段”的属性.
[System.Runtime.Serialization.DataContract(IsReference = true)] [System.ComponentModel.DataAnnotations.ScaffoldTable(true)] public class TestModel { [Display(Name="Schedule Name")] [required] public string scheduleName { get; set; } }