How do I access the ModelState from within my View (aspx page)?
-
1I don't think you should. That sort of thing should happen in the Controller.– Matthew GrovesJun 26, 2009 at 13:38
-
1I need to know if my ModelState is valid. I don't want to do this: <% if (Html.ValidationSummary() != null) %> <% { %> <p class="validation-summary-errors"> Your message was not sent. Please correct the errors below and try again. </p> <% } %> Instead I want to do this: <% if (!ViewData.ModelState.IsValid) { %> <p class="validation-summary-errors"> Your message was not sent. Please correct the errors below and try again. </p> <%} %>– Saajid IsmailJun 26, 2009 at 13:44
-
12That sounds like a good enough reason to access ModelState– BritishDeveloperJun 29, 2011 at 8:29
-
There's absolutely nothing wrong with accessing ModelState in the view. It's part of ViewData.– kennythecoderSep 12, 2016 at 13:31
Add a comment
|
1 Answer
Use ViewContext.ViewData.ModelState
.
-
21Also worthy to note that you can just do
ViewData.ModelState
and if you want to display some conditional markup on errors you can do like this:@if (!ViewData.ModelState.IsValid)
Sep 15, 2013 at 2:16