How do I get the collection of Model State Errors in ASP.NET MVC?
Posted
by Ryan Montgomery
on Stack Overflow
See other posts from Stack Overflow
or by Ryan Montgomery
Published on 2009-02-21T16:17:50Z
Indexed on
2010/06/16
11:52 UTC
Read the original article
Hit count: 348
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 and if any display them in specific format. Also on the input controls I want to check for a specific property error and add a class to the input.
P.S. I'm using the Spark View Engine but the idea should be the same.
So I figured I could do something like...
<if condition="${ModelState.Errors.Count > 0}">
DispalyErrorSummary()
</if>
....and also...
<input type="text" value="${Model.Name}" class="?{ModelState.Errors["Name"] != string.empty} error" />
....
Or something like that.
UPDATE
My final solution looked like this:
<input type="text" value="${ViewData.Model.Name}" class="text error?{!ViewData.ModelState.IsValid && ViewData.ModelState["Name"].Errors.Count() > 0}" id="Name" name="Name" />
This only adds the error css class if this property has an error.
© Stack Overflow or respective owner