Search code examples
asp.net-mvc-3viewdataviewbag

MVC3 Passing data from controller to another controller


Passing small amount of data from one controller to another controller, do I use ViewBag? ViewData? or Session...etc... what is the right approach.

e.g. My First [Controller1]/[Action: Register] generates the userid and then needs to be redirected to another controller

return RedirectToAction("Create", "Controller2");

Then on Create (HttpPost) I am storing the data + plus the userid to the database and then redirecting to the next controller which acts the same way on HttpPost it stores the data + plus the userid to the database.

I am a bit lost what approach should I take, how do I pass the userid in a secure way/right way?, please advice, Thank you


Solution

  • If its not private, just pass it on the url using the third parameter in your redirect: new {id=whateverValue}

    If it is private data, use TempData - it will be removed after the next request reads it (well at the end of the request)