Asp.net 查找UpdateModel失败原因

Asp.net 查找UpdateModel失败原因,asp.net,asp.net-mvc-5,Asp.net,Asp.net Mvc 5,在创建视图页面中,所有jquery验证都通过,当创建对象进入操作时,UpdateModel失败。我是否可以找到更新失败的字段?通过在调试模式下观看“e” try { UpdateModel(house_info); } catch (Exception e) { throw e; } 您可以检查ModelState是否存在错误。下面将为您提供每个有错误的属性以及与该属性关联的第一个错误的列表 var errors = ModelState.Keys.Where(

在创建视图页面中,所有jquery验证都通过,当创建对象进入操作时,UpdateModel失败。我是否可以找到更新失败的字段?通过在调试模式下观看“e”

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

您可以检查
ModelState
是否存在错误。下面将为您提供每个有错误的属性以及与该属性关联的第一个错误的列表

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

您可以检查
ModelState
是否存在错误。下面将为您提供每个有错误的属性以及与该属性关联的第一个错误的列表

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

此外,
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

}

此外,
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

}