How do I access the ModelState from within my View (aspx page)?
Asked Answered
A

1

127

How do I access the ModelState from within my View (aspx page)?

Akira answered 26/6, 2009 at 13:36 Comment(4)
I don't think you should. That sort of thing should happen in the Controller.Wordbook
I 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> <%} %>Akira
That sounds like a good enough reason to access ModelStateRosser
There's absolutely nothing wrong with accessing ModelState in the view. It's part of ViewData.Cystocele
A
196

Use ViewContext.ViewData.ModelState.

Autecology answered 26/6, 2009 at 13:37 Comment(1)
Also 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)Prickle

© 2022 - 2024 — McMap. All rights reserved.