TempData 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
// Summary:
// Gets or sets the dictionary for temporary data.
//
// Returns:
// The dictionary for temporary data.
public TempDataDictionary TempData { get; set; }
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{ public class TestController : Controller
{
public ActionResult Index()
{ TempData["test"] = "hi Tempdata";
return View();
}
} }
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
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
When we run the above code we will get output like as shown below