I have one ViewModel
which has 8 properties and 6 of them are complex types. And I must display them in 3 groups and to save all data to database at last. Now, I have created two methods for this purpose, the first will be decorated with HttpGet
and the second one with HttpPost
. I have already created all these 3 groups in one .cshtml
. For the first time, only the first group is visible. And after clicking NEXT button, I will make the second group visible and the first group will be invisible with the help of Javascript/Jquery. And then the third group will be visible. And in this last part submit button must be appeared. After clicking this, I will post whole model back to the action method. But, in this scenario I have two problems:
- I must validate only visible fields with client side validation
- If somehow the client side validation passed and the server side is not, then I must find and display not validated part.
So, for solving these problems, I have decided two create four action methods, which will post viewmodel back to other one. For making it clear, I want to show this like that:
- CreateViewModel
[HttpGet]
- GoToSecondPart
[HttpPost]
- GoToThirdPart
[HttpPost]
- SaveToDatabase
[HttpPost]
1 will return my viewmodel instance to 2, and 2 in his turn will post it to 3, and 3 will post it to 4.
Is it good aproach? If not, what would you recommend me?
It sounds to me that you are trying to make a wizard. It would be easier to break your pages into partials, using the same view model but displaying only the fields that are required for that page (throw the others into hidden fields to save the data). You could then make AJAX calls back to your controller to render the next / previous pages which will prevent the screen flicker making it look like the page hasn't changed.
Your current approach means multiple post/redirects and some form of temporary repository or something that mimics viewstate. A better approach would be to do everyting within one form and validate individual controls in each section using Validator.element(element)
In the Next button
.click()
event<section>
- e.g.var controls = $(this).closest('section').find('input, textarea, select');
$.each
loop, call$('form').validate().element($(this));
$(this).valid();
The final section includes the 'Save'` button which does a normal submit
View
css (hide all but the first section)
Script
This will address all client side validation without the need for multiple posts/redirects. As for what happens in the rare instance of having to return the view because the model is still invalid (the error message(s) will be generated but could be in a hidden section so not initially visible)
$('span.field-validation-error)
and the unhide the associated parent section(s)