Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is TempData, How to use in MVC, How Tempdata to use of transferring Data.
#1
Hi all
  • Use TempData when you need data to be available for the next request, only. In the next request, it will be there but will be gone after that.
  • TempData is used to pass data from the current request to the subsequent request, in other words in case of redirection. That means the value of TempData will not be null.
  • Once the value read from the data the values are not available for next request (request might be from controller to view or controller to controller or etc) we can read the values any times within that request.
  • To retain the even after reading data from one request to another request need to use the TempData.Peek(string key). After using this data will retain into the TempData until application closed.
  • After reading data from TempData if we want don’t delete data from TempData use TempData.Keep()
  • TempData.Keep() : This will not delete all data on current request completion
  • TempData.Keep(string Key) : This will not delete the data of the specific key on current request completion

Example

Model Code
namespace MvcApplication2.Models
{
    public class Customer
    {     
        public string UserName { get; set; }
    }
}
Controller Code
using MvcApplication2.Models;
 
namespace MvcApplication2.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Default/
 
        public ActionResult Index()
        {
            var customerName = new Customer
            {
                UserName = "Rohatash",
            };
           
            ViewBag.Customer = customerName;         // Defines ViewBag
            ViewData["ViewCustomer"] = customerName; // Defines ViewData
            TempData["TempCustomer"] = customerName;  // Defines TempData
            return View();                      
        }
View Code
@model MvcApplication2.Models.Customer
@{
   
    var ViewBagTest = (ViewBag.Customer as MvcApplication2.Models.Customer).UserName;
    var ViewDataTest =     ViewData["ViewCustomer"] as MvcApplication2.Models.Customer).UserName;
    var TempDataTest = (TempData["TempCustomer"] as MvcApplication2.Models.Customer).UserName; 
             
    <div>
        <h1>@ViewBagTest</h1>
        <h2>@ViewDataTest</h2>
        <h3>@TempDataTest</h3>
    </div> 
}
 
Next Request (Redirection) Code
Now see the example of a Next Request to understand the TempData object. In the Next Request (Redirection), the TempData value does not  become null. But in the case of ViewData and ViewBag the value becomes null. Now change the CustomerController code to make a redirection (Next Request).
Now the CustomerController Code is as the following: 
namespace MvcApplication2.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Default/
 
        public ActionResult Index()
        {
            var customerName = new Customer
            {
                UserName = "Rohatash",
            };
 
            ViewBag.Customer = customerName;         // Defines ViewBag
            ViewData["ViewCustomer"] = customerName; // Defines ViewData
            TempData["TempCustomer"] = customerName;  // Defines TempData           
            return RedirectToAction("TempDataTest");
        }
 
        public ActionResult TempDataTest()
        {
 
            String ViewBagTest = ViewBag.Customer;  // After the redirect, ViewBag contains null value.
            String ViewDataTest = (string)ViewData["ViewCustomer"]; // After the redirect, ViewData also contains null value.
            String TempDataTest = TempData["TempCustomer"].ToString(); //After the redirect, Only TempData survives a redirect      
            return View("Index");
        }
    }
}
The only string TempDataTest has the data other two having only null.

Hope this migth hepls you.
Thank You.
Reply
#2
Suppose I used Session value in TempData.Peek(string Key) ,then is that must to use TempData.Keep(string key)/TempData.Keep() !?
Reply
#3
Hi Yashoda,
No need to use the TempData.Keep(string key)/TempData.Keep(), whenever you use TempData.Peek(String Key).
when you make any TempData value as Peek i.e TempData.Peek(string Key).
which means :After using this data will retain into the TempData until application closed.

Use the TempData.Keep(string key)/TempData.Keep() when you don't use TempData.Peek(string Key).

Difference between TempData.Keep(string key) and TempData.Peek(string Key).
TempData.Keep(string key) : Keeps the value only for next request.
TempData.Peek(string Key) : Keeps the Value until application get closed

Hope it helps.
Reply
#4
Thanq u Mahesh Smile
Reply
#5
Nice Information Mahesh!
Santosh Kokatnur
C#, .NET Developer,
Banglore
India.
[Image: logo.jpg]
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  How get data from query string in MVC from Razor View Engine Mahesh 0 885 26-09-2015, 01:05 PM
Last Post: Mahesh

Forum Jump:


Users browsing this thread: 1 Guest(s)