How to send data from controller to view
what is the use of TempData in Asp.Net Mvc
1782
18-Dec-2019
Updated on 18-Dec-2019
Shikhar Arora
18-Dec-2019TempData in MVC is used to pass data from Controller to Controller or Controller to view and it is used to pass data from the current request to the next request.
TempData has a short life and it required typecasting. Following is the code scrap of TempData in MVC
TempData is a dictionary object that is obtained from the TempDataDictionary class. We will see how we can assign value to TempData in Controller with a simple example for that create one new controller in the application and write code like as shown below
Now we will create one view to show data for that right click on controller à select add view give the name for that view (Index.cshtml). Open view file and write the following code to show data in the website
@{ViewBag.Title = "Index";
}
<h2>Index</h2>
@TempData["test"]
When we run the above code we will get output like as shown below