標籤:
在ASP.NET MVC架構中沒有了自己的控制項,頁面顯示完全就回到了寫html代碼的年代。還好在asp.net mvc架構中也有內建的HtmlHelper和UrlHelper兩個協助類。另外在MvcContrib擴充項目中也有擴充一些協助類,這樣我們就不光只能使用完整的html來編寫了需要顯示的頁面了,就可以使用這些協助類來完成。
1.ActionLink
<div> @Html.ActionLink("這是一個測試", "Other", "Home"); 帶有QueryString的寫法 @Html.ActionLink("這是一個測試", "Other", "Home", new { page = 1 }); @Html.ActionLink("這是一個測試", "Other", "Home", new { page = 1 }, null); QueryString與Html屬性同時存在 @Html.ActionLink("這是一個測試", "Other", "Home", new { page = 1 }, new { id = "link1" }) @Html.ActionLink("這是一個測試", "Other", new { page = 1 }, new { id = "link1" }) 實際用途(彈出新視窗) @Html.ActionLink("這是一個測試", "Other", "Home", new { page = 1 }, new { target = "_blank" }); </div>
產生結果:
<div> <a href="/Home/Other">這是一個測試</a>; 帶有QueryString的寫法 <a href="/Home/Other?Length=4" page="1">這是一個測試</a>; <a href="/Home/Other?page=1">這是一個測試</a>; QueryString與Html屬性同時存在 <a href="/Home/Other?page=1" id="link1">這是一個測試</a> <a href="/Home/Other?page=1" id="link1">這是一個測試</a> 實際用途(彈出新視窗) <a href="/Home/Other?page=1" target="_blank">這是一個測試</a>; </div>
2.RouteLink 跟ActionLink功能一樣
<%=Html.RouteLink("關於", "about", new { })%> 帶QueryString <%=Html.RouteLink("關於", "about", new { page = 1 })%> <%=Html.RouteLink("關於", "about", new { page = 1 }, new { id = "link1" })%> 產生結果: <a href="/about">關於</a> <a href="/about?page=1">關於</a> <a href="/about?page=1" id="link1">關於</a>
3.Form表單
<div> @using (Html.BeginForm("Other", "Home", FormMethod.Post, new { id = "myForm" })) { } </div>
產生結果:
<div> <form action="/Home/Other" id="myForm" method="post"></form> </div>
4.TextBox和TextBoxFor
TextBox和TextBox的不同是,TextBoxFor裡面可以存放Model的欄位值,而TextBox不可以
<div> @Html.TextBox("txtName", "ChenChunXiao", new { style = "width:300px" }) @Html.TextBoxFor(a => a.CategoryName, new { @style = "width:300px;" }) </div>產生結果 <input id="txtName" name="txtName" style="width:300px" type="text" value="ChenChunXiao" /> <input id="CategoryName" name="CategoryName" style="width:300px;" type="text" value="Beverages" />
參考
常用的:http://www.cnblogs.com/fishtreeyu/archive/2011/03/23/1992498.html
擴充方法:http://www.cnblogs.com/wenjiang/archive/2013/03/30/2990854.html
http://www.cnblogs.com/longgel/archive/2010/02/03/1662894.html
ASP.NET MVC- HtmlHelper的用法