首页 > > > 关于MVC中TryUpdateModel使用
Model
publicclass People
{
[Required]
publicstringname {get;set; }
[Required]
publicstringphone {get;set; }
[Required]
publicstringaddress {get;set; }
}
Web页面

在点击提交按钮的时候进入UpdatePeople方法

[HttpPost]
public ActionResult UpdatePeople(FormCollection form)
{
People people =new People();
if(TryUpdateModel
{
//更新数据库操作 }
else {
//该怎么判断哪些值出错了
//比如说name没有值,是使用var error= ModelState["name"].Errors[0]这样的方式一个个进行判断吗?
//如果窗体上值很多,会不会很麻烦
}
return View();
}

如上else中注释所示,当if中条件不满足时,else中如何取得错误信息?
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3598/viewspace-3134322/ ,如需转载,请注明出处,否则将追究法律责任。