asp.net-mvc 如何从 Razor 视图中检查 ModelState.IsValid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8506960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check ModelState.IsValid from inside my Razor view
提问by Samantha J T Star
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>
回答by Darin Dimitrov
Is model state available in the view?
模型状态在视图中可用吗?
Of course:
当然:
@if (!ViewData.ModelState.IsValid)
{
<div>There are some errors</div>
}
回答by vcsjones
It's not common to need this in the view itself, but you can access it like so:
在视图本身中需要它并不常见,但您可以像这样访问它:
@ViewData.ModelState.IsValid

