ModelState.isValid错误

Dot*_*row 0 asp.net-mvc entity-framework-4.1 asp.net-mvc-3

我已经创建了一个Edit Action方法,但它不在ModelState.isValid中.我该如何检查错误?

  public PartialViewResult UpdateAccountDetails(string accountNumber)
  {
      CreditReportService crService = new CreditReportService();

      AccountInfo account = new AccountInfo();
      account.Account = service.GetAccountDetails(accountNumber);
      account.AccountStatuses = service.GetAccountStatuses();
      account.AccountTypes = service.GetAccountTypes();
      account.CreditTerms = service.GetCreditTerms();

      return PartialView("_UpdateAccountDetails", account);
  }

  [HttpPost]
  public ActionResult UpdateAccountDetails(Account account)
  {
      if (ModelState.IsValid)
      {
          service.SaveAccount(account);
          TempData["message"] = "Account has been updated successfully!";

          AccountInfo accountInfo = new AccountInfo();
          accountInfo.AccountStatuses = service.GetAccountStatuses();
          accountInfo.AccountTypes = service.GetAccountTypes();
          accountInfo.CreditTerms = service.GetCreditTerms();
          return PartialView("_UpdateAccountDetails", accountInfo);
      }
      else
      {
          return PartialView("_UpdateAccountDetails", account);
      }
  }
Run Code Online (Sandbox Code Playgroud)

Kal*_*exx 10

通过访问ModelState.Errors集合.该集合包含ModelError项的集合,其中包含错误消息和引发模型错误的异常.


编辑: 我想我应该看看自己.似乎控制器ModelState实际上是一个(字典)的集合ModelState.要获得所有错误,您应该能够通过以下方式获取所有ModelError类的实例:

var errors = ModelState.Select(x => x.Value.Errors).ToList();
Run Code Online (Sandbox Code Playgroud)