Finished 'TempData in ASP.NET MVC'

This commit is contained in:
jason.zhu 2021-06-15 22:21:54 +10:00
parent 7433bb0d13
commit f826210b5f

View File

@ -11,38 +11,42 @@ namespace FirstMVCDemo.Controllers
{ {
public class EmployeeController : Controller public class EmployeeController : Controller
{ {
// GET: Employee public ActionResult Method1()
public ActionResult Details()
{ {
//Employee Basic Details TempData["Name"] = "Pranaya";
Employee employee = new Employee() TempData["Age"] = 30;
{ return View();
EmployeeId = 101, }
Name = "Dillip",
Gender = "Male", public ActionResult Method2()
Department = "IT", {
Salary = 10000, string Name;
AddressId = 1001 int Age;
};
//Employee Address if (TempData.ContainsKey("Name"))
Address address = new Address() Name = TempData["Name"].ToString();
{
AddressId = 1001, if (TempData.ContainsKey("Age"))
City = "Bhubaneswar", Age = int.Parse(TempData["Age"].ToString());
State = "Odisha",
Country = "India", TempData.Keep();
Pin = "755019"
}; // do somethign with userName or userAge Ahere
//Creating the View model return View();
EmployeeDetailsViewModel employeeDetailsViewModel = new EmployeeDetailsViewModel() }
{
Employee = employee, public ActionResult Method3()
Address = address, {
PageTitle = "Employee Details Page", string Name;
PageHeader = "Employee Details", int Age;
};
//Pass the employeeDetailsViewModel to the view if (TempData.ContainsKey("Name"))
return View(employeeDetailsViewModel); Name = TempData["Name"].ToString();
if (TempData.ContainsKey("Age"))
Age = int.Parse(TempData["Age"].ToString());
return View();
} }
} }
} }