How can I check ModelState.IsValid from inside my Razor view

asp.net Mvcasp.net Mvc-3Razor

asp.net Mvc Problem Overview


I have the following in my action method:

       if (!ModelState.IsValid)
        return View(vm);

In the view I want to not present a submit key to allow deletion if the model state is not valid. Is there a way that I can do this? Is model state available in the view?

Update: I have implemented this based on the answers I was given:

            <div class="adm_td0" style=" padding: 0;">  
            @if (ViewData.ModelState.IsValid) {
                <input type='submit' value='Delete' name='SubmitAction' />
            }
                <input type='submit' value='Cancel' name='SubmitAction' />
            </div>

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

> Is model state available in the view?

Of course:

@if (!ViewData.ModelState.IsValid)
{
    <div>There are some errors</div>
}

Solution 2 - asp.net Mvc

It's not common to need this in the view itself, but you can access it like so:

@ViewData.ModelState.IsValid

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamantha J T StarView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvcvcsjonesView Answer on Stackoverflow