ASP.NET MVC- HtmlHelper的用法

來源:互聯網
上載者:User

標籤:

  在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的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.