How can I check ModelState.IsValid from inside my

2019-02-01 03:19发布

This question already has an answer here:

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>

2条回答
SAY GOODBYE
2楼-- · 2019-02-01 03:38

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

@ViewData.ModelState.IsValid
查看更多
叼着烟拽天下
3楼-- · 2019-02-01 03:57

Is model state available in the view?

Of course:

@if (!ViewData.ModelState.IsValid)
{
    <div>There are some errors</div>
}
查看更多
登录 后发表回答