Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

asp.net mvc - How can I check if my model is valid from inside the razor view?

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 like

@if ( Model.IsValid ) {

}

but the above does not work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")) {
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...