Description:How to set Viewbag before making a redirection call?
or need to know if there is a mistake in Displaying ViewBag after RedirectToAction ?
I have set the value of my viewbag variable in the controller method then use the RedirectToAction method for the redirection but when trying to use the value of the variable in my view that is always returning null value.I have use following code in my controller method.
public ActionResult Index1 () {
//some code logic placed here
ViewBag.Myvariable = "Some value";
return RedirectToAction("Home");
}
Posted by: Khalid Abbas | Posted on: Jul 16, 2018
3
Actually you are using "RedirectToAction" method which redirect you to another action "Home" in your case. In MVC to pass data between controller methods you need to use the TempData variables and for passing data from controller method to view you can use viewbag. So in your case what you are trying to achieve can be achieved by using the following code.
To get more clarity you can read the the difference between ViewData,ViewBag and TempData
Replied by: Tabish Usman | Replied on: Jul 17, 2018