ASP.NET MVC How to convert ModelState errors to json
Posted
by JK
on Stack Overflow
See other posts from Stack Overflow
or by JK
Published on 2010-05-16T23:09:00Z
Indexed on
2010/05/16
23:10 UTC
Read the original article
Hit count: 633
How do you get a list of all ModelState error messages? I found this code to get all the keys: ( http://stackoverflow.com/questions/888521/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...
© Stack Overflow or respective owner