AnthonyMascia February 2016

Is there any way to check ModelState.IsValid on page load BEFORE the form is submitted?

So we added a required field to our form that our users submit and are able to edit. So if a user were to open their form to edit it, there would be a new required field, which works fine. If you try to submit the form, a model error displays saying they need to fill it out. Fine.

But they have no indication that the field is required. As far as I know, ModelState.IsValid only triggers on a form POST. Is there anyway to call it right on page load so they can see right away that they need to fill out that new field?

Answers


Bardo February 2016

What you are asking for should be managed by model in MVC pattern.

You can use a framework for client validation, as recommended on comments, or just elaborate your own client validation.

Just keep in mind that your model need to communicate to the view which fields are mandatory, so the view can translate that information to the user through the UI.

You can do this in any way you want to do it: just using different styles for mandatory fields (which is not a very good option), programming validation rules which throw a message when a mandatory field is left empty, adding text that explains that the field is mandatory, etc...

Post Status

Asked in February 2016
Viewed 2,900 times
Voted 10
Answered 1 times

Search




Leave an answer