c# - ASP.NET MVC How to convert ModelState errors to json -


How do you get a list of all modelstate error messages? I received this code to get all the keys: ()

  var errorKeys = (item from ModelState where item.Value.Errors.Any () select the key). ToList ();  

But how can I get the error message as an IList or IQueryable?

I can go to:

  foreach (key in key key error) {string msg = modelstate [error]. King [0] .ErrorMessage; ErrorList.Add (MSG); }  

But it is doing manually - is there a way to use LINQ definitely? .ErrorMessage is far from the property chain, I do not know how to write LINQ ...

you You may want to do anything inside :

  var errorList = (item from item where item Value.Errors.Any (.). Value.Errors [0] .ErrorMessage) .Oolist ();  

Edit : You can remove individual errors in different errors by adding a to section such as:

error message).

or:

  var error list = modelstate value. Select (m = & gt; m.Errors). Select (E => E. error message). To create the list ();  

2 edit : You are searching for a dictionary; String, string []> :

  var errorList = ModelState.ToDictionary (kvp = & gt; kvp.key, kvp = & gt; kvp.Value.Errors.Select (E => E ERRAM) .ORARA ());  

Comments