Reputation: 27996
I have created an Edit Action method but it is not going inside ModelState.isValid. How can I check the error?
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);
}
}
Upvotes: 0
Views: 4539
Reputation: 685
var errors = var errors = ModelState.Where(m=>m.Value.Errors.Any()).Select(m => m.Value.Errors).ToList();
To only get a list of the errored fields only, rather than all fields and error list (exclude Errors length == 0).
Upvotes: 1
Reputation: 27803
By accessing the ModelState.Errors collection. The collection contains a collection of ModelError items, which contain the error message and exception that was thrown to cause the model error.
ModelState
is actually a (dictionary) collection of ModelState
's. To get all the errors, you should be able to get all instances of the ModelError
classes via:
var errors = ModelState.Select(x => x.Value.Errors).ToList();
Upvotes: 10