1. 自MVC架構升級到3版本以後,Razor 引擎佔主流。Razor的簡單文法如下:
@變數
@對象.屬性
@{ //程式碼片段 }
@{ @:程式碼片段中的文本 }
@{ <text>程式碼片段中有大量的文本</text>}
@if() { //程式碼片段 }
如果想向視圖中加入一些動態內容,可以有以下幾種方法,a ,inline code ; b , HTML helper ; c , Partial View 。接下來我們單獨討論每一種方法。
2. inline code
2.1 在View中引入命名空間: @using Project.Models ,這種方法可以將這個命名空間引入到這個單獨的View中,如果要想在每一個View中都引入同一個命名空間,可以在Views檔案夾下的Web.config (注意,不是更目錄下的)檔案中修改配置,如下:
<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="Project.Models" </namespaces> </pages> </system.web.webPages.razor>
2.2 理解 Razor HTML String 編碼
假設有一個類
public class ClassA { public string MyMethod() { return "<form> This is a form ! </form>"; } }
如果頁面中引用了 @classA.MyMethod()的話,那麼,這個方法的傳回值在View中將會被自動編碼成"<form> This is a form ! <form> "。MVC架構之所以這麼做,是為了保證頁面的安全性。不過,如果程式員確實像讓這個方法返回一個form表單的話,MVC架構也是可以做到的,可以這樣寫:return new MvcHtmlString("<form> This is a form ! </form>") ;
3. 使用 HTML Helper
3.1 建立一個 Inline HTML helper 方法
@{ ViewBag.Title = "Index"; } @helper CreateList(string[] items) { <ul> @foreach(string item in items) { <li> @item </li> } </ul> } <p>Fruits i Like :</p> @*在下面調用*@ @CreateList(new string[]{"蘋果","柚子","草莓","火龍果"})
3.2 建立一個External HTML helper 方法
如果建立一個行內的 HTML helper的話,只能在一個特定的頁面使用該方法,如果想建立能用於任何頁面的 HTML helper 方法的話,可以寫一個擴充方法 ,第一個參數必須是 HtmlHelper ,傳回值是MvcHtmlString。如下所示
public static class HelperClass { public static MvcHtmlString List(this HtmlHelper helper, string[] listItems) { TagBuilder ulTag = new TagBuilder("ul"); foreach (string item in listItems) { TagBuilder liTag = new TagBuilder("li"); liTag.SetInnerText(item); ulTag.InnerHtml += liTag.ToString(); } return new MvcHtmlString(ulTag.ToString()); } }
@*在下面調用*@
@*先引入HelperClass所在的命名空間*@
@using MvcArea1.Models @{ ViewBag.Title = "Index";}@Html.List(new string[]{"蘋果","柚子","草莓","火龍果"})
3.3 使用MVC架構內建的HTML Helper 方法
a , 建立Form
@using (@Html.BeginForm()){
}
b, 建立 input Helper
還有一個比較有意思的地方,如果我在Controller中設定 ViewBag.Message = "Hello , world !" ,然後在頁面中輸入 @Html.TextBox("Message") ,那麼,會產生如下的HTML代碼:
<input id="Message" name="Message" type="text" value="Hello , world !" /></form>
MVC架構會尋找 ViewBag.Message , ViewData["Message"] , @Model.Message ,如果找到存在Message屬性的值,就會賦給這個文字框的Value屬性
c ,使用強型別的 Input Helper 。
d, 使用列表 Helper
e , 使用 URL 或超連結
4. 使用Section
section 就是一塊HTML程式碼片段,在一個View裡面定義,在 Layout 頁面引用。如:
首先,在View頁面裡面引用
@{ ViewBag.Title = "Index";}<h2>Index</h2>@section Footer{ <h4>It's footer here !</h4>}然後在 Layout 頁面裡引用<!DOCTYPE html><html><head> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script></head> <body> @RenderBody() @RenderSection("Footer")</body></html>
5. 使用 PartialPage
選中 Views 目錄的某一類別檢視的檔案夾,右鍵選中 添加 => 視圖 在彈出的頁面中,選中 "建立為"分部視圖。
在新增的頁面中添加需要的HTML元素,然後在其他的頁面中引用,引用方式如下:
@Html.Partial("MyPartial")
Partial View 也可以建立為強型別的 ,具體操作這裡就不贅述了,方式跟建立強型別的頁面一樣,引用的時候,@Html.Partial 有針對強型別的重載版本。