我正在使用.NET MVC 5构建网站。
我有一个包含以下内容的ViewModel
public class ContactPageModel
{
public string Selection{ get; set; }
public string MonthlyCost { get; set; }
public ContactInfo Contact { get; set; }
}
我的数据注释位于ContactInfo
中public class ContactInfo
{
[Display(Name = "First Name: ")]
[Required(ErrorMessage = "First Name Required")]
public string FirstName { get; set; }
[Display(Name = "Last Name: ")]
[Required(ErrorMessage = "Last Name Required")]
public string LastName { get; set; }
[Display(Name = "State: ")]
[Required(ErrorMessage = "State Required")]
public string State { get; set; }
[Display(Name = "Phone Number: ")]
[Required(ErrorMessage = "Phone Number Required")]
public string Phone { get; set; }
[Display(Name = "Email: ")]
[Required(ErrorMessage = "Email Required")]
public string Email { get; set; }
[Display(Name = "Best Contact Time: ")]
[Required(ErrorMessage = "Best Contact Time Required")]
public string BestContractTime { get; set; }
}
如何正确设置数据注释,以便视图正确使用它们?
以下是我的一些观看代码:
<div class="form-group">
@Html.LabelFor(model => model.Contact.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Contact.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Contact.LastName, "", new { @class = "text-danger" })
</div>
</div>
我确实通过web.config中的ClientValidationEnabled和UnobtrusiveJavaScriptEnabled键启用了客户端验证。
我没有得到验证消息。我假设这是因为我有ContactInfo类与ContactPageModel类嵌套。