找到UpdateModel失败原因

时间:2014-09-15 07:30:51

标签: asp.net asp.net-mvc-5

在创建视图页面中,所有jquery验证都通过,当create对象传递给action时,UpdateModel失败。无论如何,我可以找到明确无法更新的字段?通过在调试模式下观看“e”?

 try { 
      UpdateModel(house_info); }
 catch (Exception e) 
     { throw e; } 

2 个答案:

答案 0 :(得分:4)

您可以检查ModelState是否有错误。以下内容将为您提供每个属性的列表,其中包含错误以及与属性相关的第一个错误

var errors = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0)
  .Select(k => new
  {
    propertyName = k,
    errorMessage = ModelState[k].Errors[0].ErrorMessage
  });

答案 1 :(得分:0)

此外,ModelState具有.IsValid属性,您应该检查该属性而不是使用异常处理。

控制器动作可能如下所示:

public void MyAction() {

    if(ModelState.IsValid) {
        // do things
    } 

    // error handling, perhaps look over the ModelState Errors collection
    // or return the same view with the 'Model' as a parameter so that the unobtrusive javascript
    // validation would show the errors on a form

}
相关问题