public class HomeController : Controller
{
public ActionResult Index()
{
var emp = new Employee
{
EmpID=101,
Name = "Deepak",
Salary = 35000,
Address = "Delhi"
};
ViewData["emp"] = emp;
ViewBag.Employee = emp;
return View();
}
}
View的代码如下:
@model MyProject.Models.EmpModel;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Welcome to Home Page";
var viewDataEmployee = ViewData["emp"] as Employee; //need type casting
}
<h2>Welcome to Home Page</h2>
This Year Best Employee is!
<h4>@ViewBag.Employee.Name</h4>
<h3>@viewDataEmployee.Name</h3>