How to set and get value from TempData in asp.net mvc?
General
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"]
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"]
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
$1,000 cpc keywords
81 By Junaid A |
![]() |
How to grow 1% every day
125 By Junaid A |
![]() |
The setup for the installation of visual studio is not complete. Please run the Visual Studio installer again to correct the issue.
106 By Junaid A |
![]() |
Getting error "Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine"
234 By Junaid A |
![]() |
CRUD in Asp.net MVC
130 By Ihsan Ullah |
![]() |
What is life?
92 By Junaid A |
![]() |
What is life?
135 By Shoaib Khan |
![]() |
paragraph
138 By Ihsan Ullah |
![]() |
life
108 By khubaib Ahmad |
![]() |
How to start PMP
679 By Junaid A |
![]() |
what is robots.txt
668 By Junaid A |
![]() |
What is CURL?
720 By Junaid A |
![]() |
618 By shaheer |
![]() |
635 By shaheer |
![]() |
MVC LEARNIG
262 By |
![]() |
The data connection could not be established: ETIMEDOUT - Connection attempt timed out Error: Connec
267 By |
![]() |
show hide kendo ComboBox
280 By |
![]() |
Active A Class In Layout
294 By |
![]() |
Issues opening Excel documents - File format and extension of 'orders.xls' don't match. The file cou
245 By |
![]() |
Multiple errors occurred during the operation, the first of which is displayed below. A full error l
246 By |
![]() |
asp.net mvc routing tricks
258 By |
![]() |
What is routes in AngularJS?
288 By |
![]() |
Attackers might be trying to steal your information from (for example, passwords, messages, or cred
256 By |
![]() |
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this
295 By |
![]() |
how to call a function in c#
312 By |