我有控制器动作,需要多个模型。
[HttpPost]
public ActionResult LoginTest(Respondent respondent, List<db.Educations> educations)
{
.......
}
ModelState.IsValid适用于单个模型,但我如何验证所有这些模型?
答案 0 :(得分:0)
有方法
TryValidateModel(Object)
有关完整信息,请参阅msdn。
因此你可以做类似的事情:
[HttpPost]
public ActionResult LoginTest(Respondent respondent, List<db.Educations> educations)
{
foreach(var education in educations)
{
if (!TryValidateModel(education))
return new HttpException("some education was not valid...");
}
// if you reach this, all educations where valid...
// thus more code such as saving can go here :)
}