ASP.NET MVC – Pass data from one action method to another action method

Updated on     Kisan Patel

When, we need to pass some data from one action method to another method while redirecting the user to another action method from calling action method. Then, we can pass certain data through the RedirectToAction method by object parameter.

In this kind of scenario, we can use TempData

public ActionResult Index()
{
      // once you have set the value, you will need to use it otherwise it remains in the memory and next time this action method will be called it throws error "key already exists"
     TempData.Add("YourTempData", "This data is coming from calling method.");
     return RedirectToAction("GotoMethod");
}
public ActionResult GotoMethod()
{
     return Content(TempData["YourTempData"].ToString());
}

When above action method is called, it saves the data into TempData with the key “YourTempData” and redirects to the “GotoMethod”.


ASP.NET MVC

Leave a Reply