Candor.Web.Mvc.ModelStateDictionaryExtension.ToJson C# (CSharp) Method

ToJson() public static method

Converts a model state dictionary into a Json response for a custom presentation.
public static ToJson ( this modelState ) : object
modelState this
return object
        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;
        }
ModelStateDictionaryExtension