How can I check if my model is really inside a razor?

I need to check if my model is valid from my Razor view. If it is valid, I want to show some HTML.

How can i do this. I want something like

@if ( Model.IsValid ) { } 

but the above does not work

+43
asp.net-mvc asp.net-mvc-3 razor
Dec 22 2018-11-11T00:
source share
1 answer

You can check if the ModelState is valid, but keep in mind that you are checking the correctness of the ModelState model during the web request:

 @if (ViewData.ModelState.IsValid) { ... } 

In addition, you can check the validity of the property in the model in the view:

 @if (ViewData.ModelState.IsValidField("FIELD_NAME")) { ... } 
+71
Dec 22 2018-11-11T00:
source share



All Articles