How do I get the collection of Model State Errors in ASP.NET MVC?

后端 未结 9 1298
孤城傲影
孤城傲影 2020-11-27 09:48

How do I get the collection of errors in a view?

I don\'t want to use the Html Helper Validation Summary or Validation Message. Instead I want to check for errors an

9条回答
  •  天命终不由人
    2020-11-27 10:15

    This will give you one string with all the errors with comma separating

    string validationErrors = string.Join(",",
                        ModelState.Values.Where(E => E.Errors.Count > 0)
                        .SelectMany(E => E.Errors)
                        .Select(E => E.ErrorMessage)
                        .ToArray());
    

提交回复
热议问题