How do you get a list of all ModelState error messages? I found this code to get all the keys: ( Returning a list of keys with ModelState errors)
var errorKeys = (from item in ModelState where item.Value.Errors.Any() select item.Key).ToList();
But how would I get the error messages as a IList or IQueryable?
I could go:
foreach (var key in errorKeys) { string msg = ModelState[error].Errors[0].ErrorMessage; errorList.Add(msg); }
But thats doing it manually - surely there is a way to do it using LINQ? The .ErrorMessage property is so far down the chain that I don't know how to write the LINQ...
You can use SelectMany function c# to get error message from modelstate mvc. It will generate error message string it contains modelstate errors; we can return as json and display in html element. You have to define validations inside the class as per requirement.
To clear the memory of the model state you need to use ModelState. Clear().
The error typically means that your Model doesn't meet the requirements to be validated. In your example, your FirstName , LastName , Email , Subject and Message properties are decorated with the [Required] attribute. This means that those values shouldn't be null and empty otherwise your condition if(ModelState.
You can put anything you want to inside the select
clause:
var errorList = (from item in ModelState where item.Value.Errors.Any() select item.Value.Errors[0].ErrorMessage).ToList();
EDIT: You can extract multiple errors into separate list items by adding a from
clause, like this:
var errorList = (from item in ModelState.Values from error in item.Errors select error.ErrorMessage).ToList();
Or:
var errorList = ModelState.Values.SelectMany(m => m.Errors) .Select(e => e.ErrorMessage) .ToList();
2nd EDIT: You're looking for a Dictionary<string, string[]>
:
var errorList = ModelState.ToDictionary( kvp => kvp.Key, kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray() );
Here is the full implementation with all the pieces put together:
First create an extension method:
public static class ModelStateHelper { public static IEnumerable Errors(this ModelStateDictionary modelState) { if (!modelState.IsValid) { return modelState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Errors .Select(e => e.ErrorMessage).ToArray()) .Where(m => m.Value.Any()); } return null; } }
Then call that extension method and return the errors from the controller action (if any) as json:
if (!ModelState.IsValid) { return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet); }
And then finally, show those errors on the clientside (in jquery.validation style, but can be easily changed to any other style)
function DisplayErrors(errors) { for (var i = 0; i < errors.length; i++) { $("<label for='" + errors[i].Key + "' class='error'></label>") .html(errors[i].Value[0]).appendTo($("input#" + errors[i].Key).parent()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With