一步步學習ASP.NET MVC3 (13)——HTML輔助方法

請註明轉載地址:http://www.cnblogs.com/arhat

今天老魏是在十分鬱悶,個人一個U盤丟了,心疼裏面的資料啊,所有是老魏辛辛苦苦積攢的Linux資料,太心疼,到如今心情還不是很爽。沒辦法,丟了也就丟了。但願老魏可以從服務器中找到這些備份的資料吧。css

那麼開始今天的章節,因爲前兩天比較忙,老魏更新的慢了,之後慢慢不上來吧!今天咱們要說的是ASP.NET MVC 中的HTML輔助方法。HTML輔助方法可以幫助咱們可以快速生成視圖代碼,經過HTML輔助方法能夠向像編寫C#同樣編寫HTML文件。html

這些輔助方法都位於System.Web.Mvc.Html這個命名空間,你們能夠從這個命名空間中查看這些方法。當了,因爲這些輔助方法只是用來生成html內容的,因此老魏這裏呢就再也不詳細的介紹,根據下面我舉的例子,你們能夠依葫蘆畫瓢看着幫助文檔來學習。服務器

而HTML輔助方法是HTMLHelper類的擴展方法,因此本章咱們主要來看看這些輔助方法是如何幫助咱們快速的開發視圖文件。app

1、超連接
HTMLHelper的擴展方法提供如下的擴展方法能夠生成超連接。 函數

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName);        

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes);

這麼多重載方法咱們並不須要所有的記住,而是在適當的時候使用適當的方法。咱們來學習一下經常使用的方法。post

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName);

這個方法是用來跳轉到對應的action。學習

@Html.ActionLink("這是超鏈接", "Index");
<a href="/">這是超鏈接</a>

可是須要注意的是的,actionName只能是視圖所在的控制下的Action方法。this

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues);

這個方法生成的超鏈接能夠傳遞一下參數。spa

@Html.ActionLink("這是超鏈接", "Index", new { id=1,name="department"});
<a href="/Home/Index/1?name=department">這是超鏈接</a>
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName);

這個方法生成的鏈接能夠跨controllercode

@Html.ActionLink("這是超鏈接", "Test","About");
<a href="/About/Test">這是超鏈接</a>;
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName,object routeValues, object htmlAttributes);
@Html.ActionLink("這是超鏈接", "Test", "About", new { name="apple"},null);
<a href="/About/Test?name=apple">這是超鏈接</a>;

咱們能夠經過最後一個參數來給html元素添加屬性。

@Html.ActionLink("這是超鏈接", "Test", "About", new { name = "apple" }, new { style="text-decoration:none"});
<a href="/About/Test?name=apple" style="text-decoration:none">這是超鏈接</a>;

這裏須要注意的地方是若是要給超連接添加一個class的css屬性,那麼必須在前面加上@符號,由於class是C#的關鍵字。

@Html.ActionLink("這是超鏈接", "Test", "About", new { name = "apple" }, new { style="text-decoration:none",@class="a"});
<a class=」a」 href="/About/Test?name=apple" style="text-decoration:none">這是超鏈接</a>;

表單
固然除了超連接,HTML輔助方法也提供了對錶單的支持。若是咱們使用輔助方法來生成表單能夠經過兩種方法,只是風格不一樣。
經過Html.BeginForm()方法來聲明一個表單。又有BeginForm方法的重載比較多,這裏就不一一列舉了,咱們來看一下經常使用的重載方法。你們能夠從System.Web.Mvc.Html.FormExtensions中查看一下重載的方法。

@using (Html.BeginForm()) 

{ 

<p>

        帳號:@Html.TextBox("username")

</p>

<p>

        密碼:@Html.Password("pwd")

</p>

}
<form action="/" method="post">   

 <p>        帳號:<input id="username" name="username" type="text" value="" />    </p>   

 <p>        密碼:<input id="pwd" name="pwd" type="password" />    </p>

</form>

那麼咱們能夠看到生成的表單中action的地址爲」/」。因此咱們能夠經過其餘的重載函數來生成表單。

@using (Html.BeginForm("Test","Home")) 

{ 

<p>

        帳號:@Html.TextBox("username")

</p>

<p>

        密碼:@Html.Password("pwd")

</p>

}
<form action="/Home/Test" method="post">   

 <p>        帳號:<input id="username" name="username" type="text" value="" />    </p>    

<p>        密碼:<input id="pwd" name="pwd" type="password" />    </p>

</form>

在上面的例子中,咱們使用到了表單元素,那麼這些表單元素你們能夠從System.Web.Mvc.HtmlInputExtensions中查看,這裏老魏就再也不作介紹了(由於比較簡單)。

相關文章
相關標籤/搜索