ASP.NET MVC How to convert ModelState errors to json

前端 未结 13 2084
余生分开走
余生分开走 2020-12-04 05:24

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 errorK         


        
13条回答
  •  离开以前
    2020-12-04 05:37

    I made and extension that returns string with seperator " " (you can use your own):

       public static string GetFullErrorMessage(this ModelStateDictionary modelState) {
            var messages = new List();
    
            foreach (var entry in modelState) {
                foreach (var error in entry.Value.Errors)
                    messages.Add(error.ErrorMessage);
            }
    
            return String.Join(" ", messages);
        }
    

提交回复
热议问题