I need to do a check if my model is valid from inside my Razor view. If it's valid then I want to be able to show some HTML.
我需要从我的Razor视图中检查我的模型是否有效。如果它是有效的,那么我想要显示一些HTML。
How can I do this. I want something like
我该怎么做呢?我想要这样
@if ( Model.IsValid ) {
}
but the above does not work
但上述方法并不奏效
1 个解决方案
#1
78
You can check whether or not the ModelState is valid, but keep in mind that you're only checking the validity of the ModelState at the time the web request was made:
您可以检查ModelState是否有效,但请记住,您仅在发出web请求时检查ModelState的有效性:
@if (ViewData.ModelState.IsValid) {
...
}
Additionally, you can check validatity of a property on the model in the view:
此外,您还可以在视图中检查模型上属性的有效性:
@if (ViewData.ModelState.IsValidField("FIELD_NAME")) {
...
}
#1
78
You can check whether or not the ModelState is valid, but keep in mind that you're only checking the validity of the ModelState at the time the web request was made:
您可以检查ModelState是否有效,但请记住,您仅在发出web请求时检查ModelState的有效性:
@if (ViewData.ModelState.IsValid) {
...
}
Additionally, you can check validatity of a property on the model in the view:
此外,您还可以在视图中检查模型上属性的有效性:
@if (ViewData.ModelState.IsValidField("FIELD_NAME")) {
...
}