我有一个 ViewModel,用于将数据 POST 回服务器。
[MetadataType(typeof(CompanyAdminViewModel))]
public class CompanyAdminViewModel
{
public Company Company { get; set; }
public RegisterModel User { get; set; }
public CompanyAdminViewModel()
{
}
}
公司实体有子实体:Company.CompanyContacts
public class CompanyContact
{
public int CompanyContactId { get; set; }
public int JobTitleId { get; set; }
public int CompanyId { get; set; }
public string Title { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public Nullable<DateTime> BirthDate { get; set; }
public string Gender { get; set; }
[Required]
public string Phone { get; set; }
public string Fax { get; set; }
public string Extension { get; set; }
[Required]
public string Email { get; set; }
public Nullable<DateTime> HireDate { get; set; }
public virtual Company Company { get; set; }
public virtual JobTitle JobTitle { get; set; }
public bool IsActive { get; set; }
}
当我查看 pagesource 时,为模型属性正确呈现了 data-* 属性。
<div class="editor-label">
<label for="FirstName">FirstName</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The FirstName field is required." id="FirstName" name="FirstName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="LastName">LastName</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The LastName field is required." id="LastName" name="LastName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
</div>
当我发布表单时,只有密码属性显示验证错误。当我检查 Model.IsValid 时,所有失败的验证都在集合中......
那么,为什么在尝试 POST 后表单上只显示一些验证错误?