How we use temp data (or pass values) in one controller from another controller in asp.net mvc?
Originally asked by user_2107228942 on question "how to upload image in C# MVC?"
You can pass data from one controller to another in asp.net MVC, Suppose you need to pass value from Index ActionMethod of
HomeControlller
to Index ActionMethod of Home2Controller
HomeController -Index Method Code will be
public ActionResult Index()
{
Person data = new Customer()
{
Id = 1,
Name = "Abcd"
};
TempData["mydata"] = data; // save your values in Temp data
return RedirectToAction("Index", "Home2"); //Call Index ActionMethod of Home2 controller
}
And to get values in Home2Controller using Tempdata, your code will be
public ActionResult Index()
{
Person data = TempData["mydata"] as Person ; //Get data as Person type
return View(data);
}
here Person
is Model
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly