MVC3 multi step form - How to persist model object
Asked Answered
U

2

9

I have a multi step form which uses one model object and I need to persist it between the steps. The object gets saved to the database only after the final step. I have seen people suggest using HTML.Serialize but how secure is this option?

Also my model object will grow as the user fills up the form which means the hidden form field with serialized data will add up size to my HTML output.

Whats the best practice for this kind of situation?

Upperclassman answered 3/3, 2011 at 14:1 Comment(2)
Do you have dynamic functionality in your wizard? Dynamic functionality is when a wizard shows/hides some controls or loads specific data from DB depending on the previously typed data and this logic can't be moved to client side.Desolate
I do have dynamic functionality which is why I can't move it to client side. Also the user can have multiple instance of this form he is allowed to work on at the same time so I have to be careful about isolating each model object.Upperclassman
C
8

I've use the TempData for this purpose.

You can store an object (a copy of your model data) in TempData, and use it in the next request. If it is not set-back in the next request it will be "destroyed". So you do not have to worry that it is filling up your session.

Canonist answered 3/3, 2011 at 14:23 Comment(3)
I was thinking of using combination of TempData.Peek() and TempData.Keep() methods. However as I mentioned in my comment above, the user is allowed to work on multiple instance of the same form. So how would I achieve each forms Model object isolation using TempData()?Upperclassman
Users always find a way to make your life harder. You could generate a unique number on the first page of the wizard. And use this as a part of the key for the TempData. Put this number on the html page (hidden field) to know the key.Canonist
For now I'm using TempData and a unique key to reference multiple instance of my ViewModel object. Also I've used TempData.Peek() for listing all the forms the User is working on. I'll let you know if I come up with any better solution. Thanks for you help.Upperclassman
A
0

Think about your Controller as a state machine where every action is a step in the wizard. About the persistence between steps is not big deal anyway is a simple POCO as a Dto, Command which is gonna to be send to your domain interface for validation and processing. You can store it in your IoC, Session, Cache, FileSystem ... I hope you got it. Keep it simple and clean.

Anatomist answered 3/3, 2011 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.