且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎样才能从我的Razor视图内部进行检查ModelState.IsValid

更新时间:2023-11-29 15:25:34


  

在视图中可用的模型状态?


块引用>

当然:

  @if(!ViewData.ModelState.IsValid)
{
    < D​​IV>有一些错误< / DIV>
}

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>

Is model state available in the view?

Of course:

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