Reputation: 3
I'm moving to MVC 4 and EF from a Windows Forms background and I'm having a hard time understanding something from an architectural/coding perspective.
I have a app for creating/edit work orders. There is a work order header record with multiple other records connected to the header - work tasks, customer data, job design data, etc. I want to create all of these records at the same time even though the data will be entered over multiple screens which I understand to be several views in the same controller.
The problem I'm having is in holding onto the data from each view and then doing a final save at the end of the views. Right now I'm trying to do this by passing data between the views using TempData. Is there a better way to do this? Even if I used Entity Framework in a Windows Forms app I could just hold onto the db context and continue to add records to it until I got to the end and executed db.SaveChanges(). Is there a way to do that in a controller or do I have to pass the db context around in temporary data or through params in the views?
I must be missing something because a lot of apps require the ability to make changes to multiple records and that's just not reasonable to try to fit every field in a single view.
Any suggestions are much apprecated!
Barry
Upvotes: 0
Views: 411
Reputation: 4166
If you are going for a wizard style interface, which it sounds like you are, I would create a view model that has the capacity to hold all the information you will need for the work order.
For simplicity lets say there are 3 views. Why you post a view, you just need to send the information in the view model to the next view. Be sure you are saving the information to hidden fields on the page so it is posted back in the next for post.
Here is an example of what I would do in the controller action:
[HTTPPost]
public ActionResult StepOne(ComprehensiveViewModel model)
{
// manipulate model as required
// notice we pass the partially completed model on to the next view
return View("StepTwo", model);
}
Then in the StepTwo view:
//Hiddens for the data you want to preserve from StepOne
@Html.HiddenFor(model=> model.SomeStepOneProperty) // etc.
Upvotes: 1