Monday 25 July 2016

TempData in asp.net MVC4?



TempData is used for passing the data from controller to controller or from one action to another action. It is a dictionary which is derived from TempDataDictionary. Internally it is using session. Tempdata life is very sort as compared to session because when you read the data from TempData , after that tempdata value has lost. You can preserve the  TempData value using keep and Peek method.

Example:-

public class HomeController : Controller
{
public ActionResult Index()
{
   var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}

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

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.