Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a model between several views/controllers in ASP MVC 4

I have a design question. I am implementing a purchase flow in my Web that has 4 steps:

A -> B -> C <-> D -> E
  • A: Build a PurchaseModel object and POST it to B.
  • B: Shows a purchase summary.
  • C, D: Login/register. This step is optional if the user is already logged in. The user can go from C to D and vice-versa.
  • E: Post to paypal

I need the PurchaseModel travelling from A to E, so my question is:

How can I pass the PurchaseModel between controllers/views? What is the recommended solution in this case?

NOTE: A, B, C and D are controllers that have the attribute [AllowAnonymous].

UPDATE

Would be correct to store the PurchaseModel in a session variable in STEP B, and then use it in the other controllers?

like image 696
Daniel Peñalba Avatar asked Jan 16 '14 09:01

Daniel Peñalba


1 Answers

I think an action should only take parameters that are relevant to it. In the case of registration or login I see these as separate concerns, so it would be wrong to pass a PurchaseModel to them. If you were passing data between different steps that is relevant to all steps I would do it via passing a common view model, or models that inherit from one another, but as this is not how it is in your case I would store in session. This will not be affected by logging in.

like image 67
Rob West Avatar answered Oct 02 '22 22:10

Rob West