ASP.NET MVC > Passing data

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

How to pass data from one action method to another action method?


Sometimes, we may need to pass some data from one action method to another while redirecting the user to another action method. In this case, we can pass certain data through the RedirectToAction method by objectParamter. However, there might be scenario where data may not fit in the objectParamter as those parameter must be defined into the action method we want to call.

In this kind of scenario, we can use TempData.

CONTROLLER CODE

        public ActionResult Index()
        {

            // once the value i set, we will need to use it otherwise it
            // remains in the memory and next time when this action method will be called
            // it throws error "key already exists" error
            TempData.Add("MyTempData", "This data is coming from calling method.");

            return RedirectToAction("TempDataMethod"); // Below action method will be called
        }

      public ActionResult TempDataMethod()
        {
            return Content(TempData["MyTempData"].ToString());
        }

TempData accepts the value in key and value pair. Value can be any type of object (not necessarily string or integer but it can be class or collection of classes or any different types of object).

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

TempDataMethod is accessing the TempData saved into Index method by specifying its key and returning to the View.

POINT OF INTEREST

  • Unless we retrieve the data stored in the TempData, it remains in the memory of the application so next time if Index method is called it throws following exception.

          System.ArgumentException: An item with the same key has already been added.

  • Passing data with TempData is only useful when we have this specific scenario where we are redirecting the user from one action method to another.
Once we retrieve the TempData, it's value is automatically removed from memory.

Retaining TempData Value

In case we need to retain the TempData after reading it's value for further use, we can either use Keep or Peek methods.

  1. Keep - Look at following code snippet, we have called the Keep method just after reading the TempData. In this case, Keep method instruct the MVC that retain the value of TempData for next request

    string data = TempData["MyTempData"].ToString();
    TempData.Keep("MyTempData"); // above like is mandatory
     
  2. Peek - In case we want to retrieve data from TempData and retain them for next request, we can directly use Peek instead of above code snippet.

    So Peek is the combination of retrieval and Keep method (above code snippet)

    string data = TempData.Peek("MyTempData").ToString();
     

 Views: 69584 | Post Order: 58



Write for us






Hosting Recommendations