Finished 'TempData in ASP.NET MVC'

master
jason.zhu 2021-06-15 22:21:54 +10:00
parent 7433bb0d13
commit f826210b5f
1 changed files with 35 additions and 31 deletions

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();
}
public ActionResult Method2()
{ {
EmployeeId = 101, string Name;
Name = "Dillip", int Age;
Gender = "Male",
Department = "IT", if (TempData.ContainsKey("Name"))
Salary = 10000, Name = TempData["Name"].ToString();
AddressId = 1001
}; if (TempData.ContainsKey("Age"))
//Employee Address Age = int.Parse(TempData["Age"].ToString());
Address address = new Address()
TempData.Keep();
// do somethign with userName or userAge Ahere
return View();
}
public ActionResult Method3()
{ {
AddressId = 1001, string Name;
City = "Bhubaneswar", int Age;
State = "Odisha",
Country = "India", if (TempData.ContainsKey("Name"))
Pin = "755019" Name = TempData["Name"].ToString();
};
//Creating the View model if (TempData.ContainsKey("Age"))
EmployeeDetailsViewModel employeeDetailsViewModel = new EmployeeDetailsViewModel() Age = int.Parse(TempData["Age"].ToString());
{
Employee = employee, return View();
Address = address,
PageTitle = "Employee Details Page",
PageHeader = "Employee Details",
};
//Pass the employeeDetailsViewModel to the view
return View(employeeDetailsViewModel);
} }
} }
} }