Reputation: 3707
I am trying to build a registration form. I have a model for all of the data for the users, but the problem is i want the form to be i "steps". So you fill out two parameters, and goes to the next step.
Now i've tried the approach to return the model in every step, and putting the earlier filled in data in hidden fields in the form, but that causes the non filled out forms to "trigger" the validation since i returned a model where only the previously filled out data is available. That is not very nice, so what would be the best approach of doing this?
Upvotes: 0
Views: 65
Reputation: 5877
As a quick solution for a small application, you could always store the model in the Session (or TempData) object:
MyModel model = new MyModel();
// Store to session
Session["myModel"] = model;
// Retrieve from session (you'll want to check for null objects)
var model = Session["myModel"] as MyModel;
For larger applications (and depending on your business rules) I'd recommend persisting the values to a data store on the completion of each form.
Note, there are trade-offs with either approach that you will need to weigh depending on your application.
Upvotes: 1
Reputation: 979
The best approach is to send down a "newed" up model with the initial request and build the model as you go. This can be easily achieved by hiding and showing pieces of your form with jQuery and storing the pieces in your model. After the model is finished (last step of your registration) you should send the completed model to the server.
<div id="Step1"><input type="submit" value="Step 2" /></div>
<div id="Step2"><input type="submit" value="Step 3" /></div>
<div id="Step3"><form><input type="submit" value="Finish" /></form></div>
jQuery to Show/Hide steps and preventDefault on Steps 1&2
Upvotes: 0