-2

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?

Daath
  • 1,899
  • 7
  • 26
  • 42
  • 2
    use the jquery validator framework. theres patterns out there. search for `[RequiredIf]` – Daniel A. White Feb 08 '16 at 13:59
  • You can always use `TryValidateModel(model);` before `return View(model);` - but why would you want to - client side validation is supposed to be unobtrusive and this would be an awful user interface –  Feb 09 '16 at 01:11

1 Answers1

1

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...

Bardo
  • 2,470
  • 2
  • 24
  • 42