初学mvc
M 代表Model 负责在数据库中存取数据
V 代表View 用来将数据展示给用户的
C 代表Controller 用来处理用户交互逻辑的
1.先在Models里创建一个Employee.cs类
public class Employee
{
public string FirstName {get;set;}
public string LastName {get;set;}
public int Salary {get;set;}
}
2.创建 Controller类 命名TestController.cs
public ActionResult GetView()
{
Employee emp=new Employee();
emp.FirstName="liu";
emp.LastName="lei";
emp.Salary=18000;
ViewData["Employee"]=emp;
return View["MyView"];
}
3.创建视图层的页面 MyView.cshtml
<div>
@{
HELLOWORLD.Models.Employee emp =(HELLOWORLD.Models.Employee) ViewData["Employee"];
}
<p>
EmployeeName:@emp.FirstName @emp.LastName
</p>
<p>
EmployeeSalary:@emp.Salary.ToString("C");
</p>
</div>
输出结果: