I have a form in MVC 4 which contains several fields and, depending on the value of a combo, I need to open a modal dialog form and load into that one 3 additional fields that will impact against the same entity that I'm creating/editing in the main form. For this modal dialog I'm using the one from jQuery UI.
Now, what I need to do is to validate (Required) the fields within the modal dialog in order to allow the user to retain the entered values which will be submited later by the main form.
My problem is how to perform the validation of those 3 fields from within the modal form (because they wouldn't be able to submit the main form until dialog is closed).
Any hints or ideas?
Regards, Cesar.
You could use AJAX to submit the form modal to the server. The modal form will have of course a separate view model associated with it. Let's exemplify:
Main view model:
Modal dialog view model:
Then you could have a controller containing 4 actions:
Next you could have a main view (
~/Views/Home/Index.cshtml
):and a partial view to contain the modal form (
~/Views/Home/Modal.cshtml
):OK, now all that's left is write some javascript to make the whole thing alive. We start by making sure that we have included all the required scripts first:
and then write our own: