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.
publicclass
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:
- The temp data is used to transfer data from Controller to View and view to controller also action method to another action method.
- For declaring action method we use following syntax:
TempData["name"] = "Ahsan";
- For transfer data on action method to another action method means with in controller we use following syntax:
name = TempData["name"].ToString();
- For show data in View we use following syntax:
@TempData["Name"]