How can I check if my model is valid from inside the razor view?

前端 未结 1 1942
面向向阳花
面向向阳花 2020-12-08 18:31

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.

How can I do this. I want something li

相关标签:
1条回答
  • 2020-12-08 18:58

    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:

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

    Additionally, you can check validatity of a property on the model in the view:

    @if (ViewData.ModelState.IsValidField("FIELD_NAME")) {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题