關于MVC,已經有了很多的讨論。這一篇我用一個簡單的執行個體示範了如何使用它,以及幾個常見問題的解答。我推薦大家要了解一下MVC,盡可能地話,提前嘗試用他做一些項目,這樣了解會更加深刻
1. 添加一個Controller。在Controllers目錄,點右鍵,有專門一個菜單項
ASP.NET MVC 實戰演練 注意:這裡的命名規範是字尾為Controller
預設生成的代碼如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication1.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
return View();
}
//
// GET: /Customer/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Customer/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Customer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Customer/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Customer/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
2. 添加一個資料模型
ASP.NET MVC 實戰演練 ASP.NET MVC 實戰演練 3. 添加視圖
首先準備一個檔案夾:Customer
ASP.NET MVC 實戰演練 選擇該檔案夾,在右鍵菜單中選擇"添加"=>"View"
ASP.NET MVC 實戰演練 點選“Add”之後,可以看到如下代碼
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage
>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index
h2>
<table>
<tr>
<th>
th>
<th>
CustomerID
th>
<th>
CompanyName
th> <th> ContactName
th> <th> ContactTitle
th> <th> Address
th> <th> City
th> <th> Region
th> <th> PostalCode
th> <th> Country
th> <th> Phone
th> <th> Fax
th>
tr> <% foreach (var item in Model) { %> <tr> <td> <%= Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) %> | <%= Html.ActionLink("Details", "Details", new { id=item.CustomerID })%>
td> <td> <%= Html.Encode(item.CustomerID) %>
td> <td> <%= Html.Encode(item.CompanyName) %>
td> <td> <%= Html.Encode(item.ContactName) %>
td> <td> <%= Html.Encode(item.ContactTitle) %>
td> <td> <%= Html.Encode(item.Address) %>
td> <td> <%= Html.Encode(item.City) %>
td> <td> <%= Html.Encode(item.Region) %>
td> <td> <%= Html.Encode(item.PostalCode) %>
td> <td> <%= Html.Encode(item.Country) %>
td> <td> <%= Html.Encode(item.Phone) %>
td> <td> <%= Html.Encode(item.Fax) %>
td>
tr> <% } %>
table> <p> <%= Html.ActionLink("Create New", "Create") %>
p>
asp:Content>
注意:這個頁面是沒有aspx.cs檔案的。這也是MVC模式極力避免的。因為如果頁面如果有代碼,就自然包含了邏輯,那麼就不是MVC了。在MVC裡面,視圖(View)顧名思義,隻是顯示内容的一個載體,它自己要不要顯示,要顯示什麼内容,全部由控制器(Controller)決定
4. 讓視圖具有實際意義。假使我們希望在Index頁面中顯示那些訂購金額在前十名的客戶名稱。
我們首先需要修改一下Index這個Action
public ActionResult Index()
{
Models.NorthwindDataContext context = new MvcApplication1.Models.NorthwindDataContext();
var query = from c in context.Customers
let total = c.Orders.Sum(
o => o.Order_Details.Sum(
d => d.Quantity * d.UnitPrice))
orderby total descending
select c;
return View(query.Take(10).ToArray());
}
5. 運作頁面看看效果如何
ASP.NET MVC 實戰演練 小結:
上面這個簡單的例子,示範了如何使用MVC這套新的開發架構。它與傳統的WebForms有兩點明顯不一樣
1. 不再基于PostBack的機制。頁面不再是首先被使用者接觸到的東西,而且頁面相對來說顯得不是那麼重要了(至少頁面名稱使用者基本不用關心了)。
2. 沒有了ViewState。其原因是因為現在的頁面中不再使用伺服器控件了。這是不是一大損失呢?初看是的,但細想一下不是。這樣做的網絡開發才更加标準。
常見問題解答
有哪幾種Action(下面列出的10個Action)
ContentResult(Content):傳回标準文本
FileContentResult(File):傳回檔案
FileStreamResult(File):傳回檔案流
FilePathResult(File):傳回檔案流
FileResult(File):傳回檔案
JavascriptResult(JavaScript):傳回javascript并在用戶端執行
JsonResult(Json):傳回json
PartialViewResult(PartialView):傳回一個局部視圖
RedirectToRouteResult(RedirectToAction):跳轉
ViewResult(View):展示一個視圖(這是用得最多的)
跳轉頁面或者執行其它的Action 其實就是跳轉Action,如果在頁面中的話,通過連結來實作 Html.ActionLink來實作,如果在服務端的話,就用RedirectToAction方法 這是需要在Action上面添加一個Attribute
[AcceptVerbs(HttpVerbs.Post)]
并且方法要有一個參數:FormCollection(這代表了表單中的域)
這是标準的Action,無需任何設定,預設情況就是GET 在頁面中的話,用Html.RendPartialView方法。在Controller中的話,用PartialView方法