简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM