在本章中,我們將討論和學(xué)習(xí)在ASP.NET MVC Framework應(yīng)用程序中構(gòu)建模型。模型存儲(chǔ)數(shù)據(jù),并根據(jù)來(lái)自控制器中的命令檢索出來(lái),最終在視圖中顯示這些數(shù)據(jù)。
Model
是一個(gè)類的集合,應(yīng)用程序中可使用這些數(shù)據(jù)來(lái)編寫業(yè)務(wù)邏輯。 因此,基本上模型是業(yè)務(wù)領(lǐng)域特定的容器。它用于與數(shù)據(jù)庫(kù)進(jìn)行交互。也可以用來(lái)操縱數(shù)據(jù)來(lái)實(shí)現(xiàn)業(yè)務(wù)邏輯。
下面通過(guò)創(chuàng)建一個(gè)新的ASP.NET MVC項(xiàng)目,來(lái)演示如何應(yīng)用模型的簡(jiǎn)單例子。
打開Visual Studio,然后單擊菜單:文件 -> 新建 -> 項(xiàng)目 選項(xiàng)。創(chuàng)建一個(gè)名稱為:MVCSimpleApp 的MVC項(xiàng)目。
詳細(xì)創(chuàng)建過(guò)程請(qǐng)參考:http://www.yiibai.com/asp.net_mvc/asp.net_mvc_getting_started.html
通過(guò)在解決方案資源管理器 中右鍵單擊 Controllers 文件夾來(lái)添加一個(gè)控件器:HomeController。在彈出菜單項(xiàng)中選擇:添加 -> 控制器 。
選擇包含讀/寫操作的MVC 5控制器 選項(xiàng),這個(gè)模板將為控制器創(chuàng)建一個(gè)具有默認(rèn)操作的Index
方法。這也將列出其他方法,如:Edit/Delete/Create 。
第1步 - 創(chuàng)建控制器
在Controllers文件夾中看到一個(gè)新的 C# 文件 - EmployeeController.cs
,在Visual Studio中打開并進(jìn)行編輯。修改更新EmployeeController.cs
文件中的代碼,其中包含一些默認(rèn)的動(dòng)作方法,如下面的代碼所示 -
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 文件夾,然后在彈出菜單項(xiàng)中選擇:添加 -> 類 , 將看到添加新項(xiàng)目對(duì)話框,填寫模型名稱為: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; }
}
}
通過(guò)添加一個(gè)方法來(lái)更新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步 - 運(yùn)行這個(gè)應(yīng)用程序,打開瀏覽器訪問(wèn)URL:http://localhost:64466/employee
,將看到以下輸出。
正如在上面的截圖所看到的,有一個(gè)錯(cuò)誤,這個(gè)錯(cuò)誤實(shí)際上是相當(dāng)具有描述性的,告訴我們它找不到索引視圖。
第6步 - 因此,要添加一個(gè)視圖,右鍵單擊Index
動(dòng)作方法,并選擇添加視圖。它將顯示“添加視圖”對(duì)話框,并將添加默認(rèn)名稱。參考下圖 -
第7步 - 從模板下拉列表中選擇列表,在模型類下拉列表中選擇Employee,并取消選中“使用布局頁(yè)面”復(fù)選框,然后單擊“添加” 按鈕。
它會(huì)在這個(gè)視圖中自動(dòng)添加一些默認(rèn)的代碼。如下所示 -
@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步 - 運(yùn)行這個(gè)應(yīng)用程序,將看到以下輸出。