如何从 Razor 视图中检查 ModelState.IsValid

我的行动方式如下:

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

在视图中,如果模型状态无效,我不想显示允许删除的提交键。有什么办法能让我做到吗?视图中的模型状态是否可用?

更新: 我已经根据我得到的答案实现了这一点:

            <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>
80217 次浏览

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

@ViewData.ModelState.IsValid

Is model state available in the view?

Of course:

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