我有以下型号
public class Person
{
public int Id {get;set;}
[Required()]
public string Name {get;set;}
[Required()]
public Address Address {get;set;}
}
public class Address
{
public int Id {get;set;}
[Required()]
public string City {get;set;}
[Required()]
public string Street {get;set;}
}
在控制器中:
[HttpPost]
public ActionResult Create(Person entity)
{
if (ViewData.ModelState.IsValid)
{
///Some code
return this.RedirectToAction("Browse");
}
else
{
return View("Edit", ViewModel);
}
}
问题是活页夹甚至尝试验证内部地址类,但我只关心 AddressID 但 ModelBinder 坚持甚至验证 City 和 Street 属性。
我怎样才能简单地覆盖原始 ModelBinder 只是为了验证内部对象的 ID(在我的情况下是 AddressID)?
有简单的方法吗?