public static object ToJson(this ModelStateDictionary modelState)
{
dynamic anon = new { };
foreach (var key in modelState.Keys)
{
anon[key] = new { name = key, value = "" };
foreach (var err in modelState[key].Errors)
{
if (!string.IsNullOrWhiteSpace(err.ErrorMessage))
anon[key].value += err.ErrorMessage;
else if (err.Exception != null)
anon[key].value += err.Exception.Message;
}
}
return anon;
}