How to set and get value from TempData in asp.net mvc?
Microsoft Net Framework

How to set and get value from TempData in asp.net mvc?

In Asp.Net mvc we use a term Temp data which means a short living instance, A temp data is used to transfer data from view to controller, controller to view, or controller to controller and on action method to another action method. It is used during present and subsequent request only.it keeps information as long as the HTTP request is to be active. In sample words we can say that temp data store data temporally and remove it after retrieving values.

 

 

Tempdata[“Name”]=”Ahsan”

 

@Tempdata[“Name”]

 

           
   
     
     
 
 

 

 

            (Controller)                                                                 (View)

 

Name=Tempdata[“Name”]

@{

TempData["Name"]="Ahsan";

}

 

           
     
     
   
 
 

 

 

 

 

            (Controller)                                                                 (View)

 

Name=Tempdata[“Name”]

Tempdata[“Name”]=”Ahsan”

 

           
     
   
     
 

 

 

 

 (Controller)                                                                (Controller)

 

The below example is show the transfer data from action to action method using Tem data.

public class TempController : Controller

{

    public ActionResult Index()

    {

        TempData["name"] = "Ahsan";

 

        return View();

    }

 

    public ActionResult About()

    {

        string name;

       

        if(TempData.ContainsKey("name"))

            name = TempData["name"].ToString();

 

        return View();

    }

 

   

}

 

In this example we have two action method name as Index and about. In index action method we define the temp data with name “Name” and assign value “Ahsan”. Its means that we have an temp data with value. In About action method we have string type variable and then we access the temp data by assign this variable. Temp data name “Name” id assign there value to string variable.

Now we take another example which transfer data from controller to view.

public class TempController: Controller
{
    public ActionResult Index()
    {
        TempData["Name"] = "Ahsan";

 
        return View();
    }

 
    
}

 

In this example we have one action method name as Index. In index action method we define the temp data with name “Name” and assign value “Ahsan”. Its means that we have an temp data with value. Now we can see how to access this in view:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

 
<lebel>My name is @TempData["Name"] </lebel>
 
For accessing the temp data in view we firstly write the sign “@”  then write the tempdata with square brackets enter name of temp data.

 
This example show how to pass list with the help of object using temp data.

 

 
List<Employee> emp = new List<Employee> 
{
 new Employee 
{
 EmployeeId = 1, 
EmployeeName = "John",
 Address = "12 Fremont St. Clermont, FL 2813", 
Phone = "+1-234-2838421" 
}
};

 
TempData["employee"] = emp;    
return View(); 

 
This example show how to pass integer using temp data.

 
In controller:

 
public ActionResult Form()
{
    TempData["sform"] = 5;
    return View();
}
In view:

 
               <p> @TempData["sform"]</p>
Result/Conclusion:
  1. The temp data is used to transfer data from Controller to View and view to controller also action method to another action method.
  2. For declaring action method we use following syntax:

 

  TempData["name"] = "Ahsan";

 

  1. For transfer data on action method to another action method means with in controller we use following syntax:

 

        name = TempData["name"].ToString();

 

  1. For show data in View we use following syntax:

 

  @TempData["Name"]

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM