How do I access the ModelState from within my View (aspx page)?
How do I access the ModelState from within my View (aspx page)?
Asked Answered
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 ModelState –
Rosser
There's absolutely nothing wrong with accessing ModelState in the view. It's part of ViewData. –
Cystocele
Use ViewContext.ViewData.ModelState
.
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.