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

后端 未结 9 1285
孤城傲影
孤城傲影 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:18

    <% ViewData.ModelState.IsValid %>
    

    or

    <% ViewData.ModelState.Values.Any(x => x.Errors.Count >= 1) %>
    

    and for a specific property...

    <% ViewData.ModelState["Property"].Errors %> // Note this returns a collection
    

提交回复
热议问题