tempdata

26 April

ASP.NET MVC offers us three options - TempData for passing data from controller to view and in next request.
tempdata
TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is the life cycle of the object. TempDatakeeps the information for the time of an HTTP Request. This means only from one page to another. This also works with a 302/303 redirection because it’s in the same HTTP Request. It helps to maintain data when you move from one controller to another controller or from one action to other action. In other words, when you redirect, “Tempdata” helps to maintain data between those redirects. It internally uses session variables. Temp data use during the current and subsequent request only means it is used when you are sure that the next request will be redirecting to next view. It requires typecasting for complex data type and check for null values to avoid an error. It is generally used to store only one time messages like error messages, validation messages.


Example: TempData

public ActionResult Index()
{
  var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["Data"] = model;
    return RedirectToAction("List");
}

public ActionResult List() 
{     
    var model= TempData["Data"];     
    return View(model); 
}

1. TempData is a dictionary object that is derived from the TempDataDictionary class and stored in short lives session.

2. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).

3. Its life is very short and lies only till the target view is fully loaded.

4. It’s required typecasting for getting data and check for null values to avoid the error.

5. It’s used to store only one time messages like error messages, validation messages
.

Persist data in TempData

The life of TempData is very short and lies only until the target view is fully loaded. But you can persist data in TempData by calling Keep() method after request completion

void Keep() - Calling this method within the current action ensures that all the items in TempData are not removed at the end of the current request. 

public ActionResult Index() 
{  
    ViewBag.Message = TempData["Message"]; 
     Student student = TempData["student"] as Student; 
    //need type casting TempData.Keep();
    //persist all strings values 
     return View(); 
}

void Keep(string key) - Calling this method within the current action ensures that specific item in TempData is not removed at the end of the current request.

public ActionResult Index() 

    ViewBag.Message = TempData["Message"]; 
     Student student = TempData["student"] as Student;  //need type        casting
   //persist only data for student key and Message key will be destroy 
   TempData.Keep("student"); 
   return View(); 
}

No comments:

Powered by Blogger.