美文网首页
MVC hello world

MVC hello world

作者: 醉酒的姑娘 | 来源:发表于2017-05-11 14:57 被阅读0次

初学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>

输出结果:

相关文章

网友评论

      本文标题:MVC hello world

      本文链接:https://www.haomeiwen.com/subject/hifbgttx.html