在本章中,我们将讨论和学习在asp.net mvc framework应用程序中构建模型。模型存储数据,并根据来自控制器中的命令检索出来,最终在视图中显示这些数据。
model是一个类的集合,应用程序中可使用这些数据来编写业务逻辑。 因此,基本上模型是业务领域特定的容器。它用于与数据库进行交互。也可以用来操纵数据来实现业务逻辑。
下面通过创建一个新的asp.net mvc项目,来演示如何应用模型的简单例子。
打开visual studio,然后单击菜单:文件 -> 新建 -> 项目 选项。创建一个名称为:mvcsimpleapp 的mvc项目。
详细创建过程请参考:http://www.h3.com/asp.net_mvc/asp.net_mvc_getting_started.html

通过在解决方案资源管理器 中右键单击 controllers 文件夹来添加一个控件器:homecontroller。在弹出菜单项中选择:添加 -> 控制器 。
选择包含读/写操作的mvc 5控制器 选项,这个模板将为控制器创建一个具有默认操作的index方法。这也将列出其他方法,如:edit/delete/create 。

第1步 - 创建控制器
在controllers文件夹中看到一个新的 c# 文件 -employeecontroller.cs,在visual studio中打开并进行编辑。修改更新employeecontroller.cs文件中的代码,其中包含一些默认的动作方法,如下面的代码所示 -
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
namespace mvcsimpleapp.controllers {
public class employeecontroller : controller{
// get: employee
public actionresult index(){
return view();
}
// get: employee/details/5
public actionresult details(int id){
return view();
}
// get: employee/create
public actionresult create(){
return view();
}
// post: employee/create
[httppost]
public actionresult create(formcollection collection){
try{
// todo: add insert logic here
return redirecttoaction("index");
}catch{
return view();
}
}
// get: employee/edit/5
public actionresult edit(int id){
return view();
}
// post: employee/edit/5
[httppost]
public actionresult edit(int id, formcollection collection){
try{
// todo: add update logic here
return redirecttoaction("index");
}catch{
return view();
}
}
// get: employee/delete/5
public actionresult delete(int id){
return view();
}
// post: employee/delete/5
[httppost]
public actionresult delete(int id, formcollection collection){
try{
// todo: add delete logic here
return redirecttoaction("index");
}catch{
return view();
}
}
}
}
第2步 - 添加模型
右键单击解决方案资源管理器 中的models 文件夹,然后在弹出菜单项中选择:添加 -> 类 , 将看到添加新项目对话框,填写模型名称为:employee.cs ,如下图所示 -

第3步 - 使用下面的代码将一些属性添加到employee类。
using system;
using system.collections.generic;
using system.linq;
using system.web;
namespace mvcsimpleapp.models {
public class employee{
public int id { get; set; }
public string name { get; set; }
public datetime joiningdate { get; set; }
public int age { get; set; }
}
}
通过添加一个方法来更新employeecontroller.cs文件,该方法将返回员工列表。
[nonaction]
public list<employee> getemployeelist(){
return new list<employee>{
new employee{
id = 1,
name = "allan",
joiningdate = datetime.parse(datetime.today.tostring()),
age = 23
},
new employee{
id = 2,
name = "carson",
joiningdate = datetime.parse(datetime.today.tostring()),
age = 45
},
new employee{
id = 3,
name = "carson",
joiningdate = datetime.parse(datetime.today.tostring()),
age = 37
},
new employee{
id = 4,
name = "laura",
joiningdate = datetime.parse(datetime.today.tostring()),
age = 26
},
};
}
第4步 - 更新索引操作方法,如下面的代码所示。
// get: employee
public actionresult index()
{
var employees = from e in getemployeelist()
orderby e.id
select e;
return view(employees);
}
第5步 - 运行这个应用程序,打开浏览器访问url:http://localhost:64466/employee,将看到以下输出。

正如在上面的截图所看到的,有一个错误,这个错误实际上是相当具有描述性的,告诉我们它找不到索引视图。
第6步 - 因此,要添加一个视图,右键单击index动作方法,并选择添加视图。它将显示“添加视图”对话框,并将添加默认名称。参考下图 -

第7步 - 从模板下拉列表中选择列表,在模型类下拉列表中选择employee,并取消选中“使用布局页面”复选框,然后单击“添加” 按钮。
它会在这个视图中自动添加一些默认的代码。如下所示 -
@model ienumerable<mvcsimpleapp.models.employee>
@{
layout = null;
}
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>index</title>
</head>
<body>
<p>
@html.actionlink("create new", "create")
</p>
<table class="table">
<tr>
<th>
@html.displaynamefor(model => model.name)
</th>
<th>
@html.displaynamefor(model => model.joiningdate)
</th>
<th>
@html.displaynamefor(model => model.age)
</th>
<th></th>
</tr>
@foreach (var item in model) {
<tr>
<td>
@html.displayfor(modelitem => item.name)
</td>
<td>
@html.displayfor(modelitem => item.joiningdate)
</td>
<td>
@html.displayfor(modelitem => item.age)
</td>
<td>
@html.actionlink("edit", "edit", new { id=item.id }) |
@html.actionlink("details", "details", new { id=item.id }) |
@html.actionlink("delete", "delete", new { id=item.id })
</td>
</tr>
}
</table>
</body>
</html>
第8步 - 运行这个应用程序,将看到以下输出。
