Reputation: 8225
I would like to know how I can flow data between multiple actions in MVC4.
For example, user is landing at one page (non-authenticated), fills in some data then goes to 2nd view, fills in another set of data, and then on submit, the code checks if the user is authenticated then proceeds further, otherwise go to login / register view, authenticate and redirect to 3rd step.
Now, I want to know how I can keep the data for that user while they are authenticating, should I put the data in session object and once the user is done with authentication retrieve the data? I'm not sure how to do it in MVC the right way, since it's a bit different compared to web forms.
Upvotes: 0
Views: 218
Reputation: 491
What you could do is to create a TempData key in your initial controller and when the value is returned, its value would be what the user has input.
In your controller action:
[HttpPost]
public ActionResult LandingPage(LandingPageViewModel model)
{
TempData["Model"] = model;
return RedirectToAction("OtherDataPage");
}
So on your landing page, when the user sends input, you store it in TempData and then redirect the user to the other page to fill information.
In the other action, you can use TempData to set object values from the user's previous input.
public ActionResult OtherDataPage()
{
LandingPageViewModel model = new LandingPageViewModel();
model = TempData["Model"];
return View();
}
Something like that should persist the user input
Upvotes: 1
Reputation: 133
You Can use "TempData" or "Viewbag" for more info you can see this link What is ViewData, ViewBag and TempData?
Upvotes: 0
Reputation: 5773
In these scenarios is not different from WebForm. You can use the Session even if usually is preferable not to store state in the server, you can also use a cookie (if the data is small).
Upvotes: 1