ASP.NET MVC中HtmlHelper控制項7個大類中各個控制項使用詳解_實用技巧

來源:互聯網
上載者:User

HtmlHelper類在命令System.Web.Mvc.Html之中,主要由7個靜態類組成,它們分別是FormExtensions類,InputExtensions類,LinkExtensions類,SelectExtensions類,TextExtensions類,ValidationExtensions類,RenderPartialExtensions類。

為了方便開發人員使用HtmlHelper控制項,在視圖ViewPage類中設定了一個屬性Html它就是HtmlHelper類型。

一.FormExtensions類

定義了3中類型的擴充方法BeginForm,BeginRouteForm,EndForm。

(1) BeginForm (實現表單定義的開始部分)

重載方法有13個:

BeginForm();

BeginForm(Object routeValues);

BeginForm(RouteValueDictionary routeValues);

BeginForm(string actionName,string controllerName);

BeginForm(string actionName,string controllerName,object routeValues);

BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues);

BeginForm(string actionName,string controllerName,FormMethod method);

BeginForm(string actionName,string controllerName,object routeValues,FormMethod method);

BeginForm(string actionName,string controllerName,RouteValueDictionary routeVaues,FormMethod method);

BeginForm(string actionName,string controllerName,FormMethod method,object htmlAttributes);

BeginForm(string actionName,string controllerName,FormMethod method,IDictionary<string,object> htmlAttributes);

BeginForm(string actionName,string controllerName,object routeValues,FormMethod method,object htmlAttributes);

BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FormMethod method,IDictionary<string,object> htmlAttributes);

對於第二個重載方法可以設定如下:

複製代碼 代碼如下:

Html.BeginForm(new{action="action",controller="actroller",id="2"});

在上述代碼中,設定了路由值的一個執行個體化對象,輸出的HTML語句是:

複製代碼 代碼如下:

<form action="actroller/action/2" method="post"/>

對於最後一個第十三個方法的最後一個參數是執行個體化對象設定相關屬性的值例如class,width等。

(2)BeginRouteForm (主要實現表單定義的開始部分,以路由的方法設定action的值)

有12個重載方法:

BeginRouteForm(object routeValues);

BeginRouteForm(RouteValueDictionary routeValues);

BeginRouteForm(string routeName);

BeginRouteForm(string routeName,object routeValues);

BeginRouteForm(string routeName,RouteValueDictionary routeValues);

BeginRouteForm(string routeName,FormMethod method);

BeginRouteForm(string routeName,object routeValues,FormMethod method);

……

對於第一個重載方法:

複製代碼 代碼如下:

Html.BeginRouteForm(new {action="action"});

複製代碼 代碼如下:

<form action="Home/action" method="post"/>Home是頁面所在的目錄

BeginForm與BeginRouteForm的區別就在於第一個的action是action第二個的action是Home/action

(3)EndForm(實現表單的定義的結束部分)

複製代碼 代碼如下:

Html.EndForm();

相當於</Form>

二.InputExtensions類有5種類型的擴充方法,可在視圖中設定checkBox,hidden,password,radioButton,textBox控制項。

(1)CheckBox 實現複選框控制項有6個重載方法

CheckBox(string name);

CheckBox(string name,bool isChecked);

CheckBox(string name,bool isChecked,object htmlAttributes);

CheckBox(string name,object htmlAttributes);

CheckBox(string name,Idictionary<string,object> htmlAttributes);

CheckBox(string name,bool isChecked,Idictionary<string,object> htmlAttributes);

設定複選框的實現代碼:

複製代碼 代碼如下:

<%=Html.BeginForm("CheckBox","Home") %>
<fieldset>
<legend>設定字型:</lengend>
<%=Html.CheckBox("MyCheckBox1",true,new{id="checkBox1"})%>
<label for="checkBox1">黑體</label>
<%=Html.CheckBox("MyCheckBox2",false,new{id="checkBox2"})%>
<label for="checkBox1">斜體</label>
<br/><br/>
<input type="submit" value="Submit"/>
</fieldset>
<%Html.EndForm();%>

運行上述代碼,上述複選框的設定代碼對應的HTML語句:

複製代碼 代碼如下:

<input checked="checked" id="checkBox1" name="MyCheckBox1" type="CheckBox" value="true"/>
<input name="MyCheckBox1" type="hidden" value="false"/>
<input id="checkBox2" name="MyCheckBox2" type="CheckBox" value="false"/>
<input name="MyCheckBox2" type="hidden" value="false"/>

在後台檢索checkBox

複製代碼 代碼如下:

public ActionResult CheckBox (FormCollection formCollection)
{
 bool MyCheckBox1=formCollection[0].Contains("true");//檢索第一個複選框是否被選中
 bool MyCheckBox2=formCollection["MyCheckBox2"].Contains("true");//檢索名字是MyCheckBox2的複選框是否倍選中
 ViewData["CheckBox1"]=MyCheckBox1;
 ViewData["CheckBox2"]=MyCheckBox2;
 return View();
}

(2)Hidden 表單中的隱藏數值,有4個重載方法。

Hidden(string name);

Hidden(string name,object value);

Hidden(string name,object value,object htmlAttributes);

Hidden(string name,object value,Idictionary<string,object> htmlAttributes);

eg:

複製代碼 代碼如下:

Html.Hidden("testName");

對應輸出的Html語句如下:

複製代碼 代碼如下:

<input id="testName" name="testName" type="hidden" value=""/>

(3)Password 主要是輸入密碼的文字框,有4個重載方法。

Hidden(string name);

Password (string name,object value);

Password (string name,object value,object htmlAttributes);

Password (string name,object value,Idictionary<string,object> htmlAttributes);

eg:

複製代碼 代碼如下:

Html.Password ("MyPwd");

對應輸出的Html語句如下:
複製代碼 代碼如下:

<input id="MyPwd" name="MyPwd" type="password" />

--------------------------------------------------------------------------------------------

HTML擴充類的所有方法都有2個參數:

以textbox為例子
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary<string, Object> htmlAttributes )
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object htmlAttributes )
這2個參數代表這個html標籤的屬性集合。使用方法如下。

1.ActionLink

複製代碼 代碼如下:

<%=Html.ActionLink("這是一個串連", "Index", "Home")%>

帶有QueryString的寫法

複製代碼 代碼如下:

<%=Html.ActionLink("這是一個串連", "Index", "Home", new { page=1 },null)%>
<%=Html.ActionLink("這是一個串連", "Index", new { page=1 })%>

有其它Html屬性的寫法

複製代碼 代碼如下:

<%=Html.ActionLink("這是一個串連", "Index", "Home", new { id="link1" })%>
<%=Html.ActionLink("這是一個串連", "Index",null, new { id="link1" })%>

QueryString與Html屬性同時存在

複製代碼 代碼如下:

<%=Html.ActionLink("這是一個串連", "Index", "Home", new { page = 1 }, new { id = "link1" })%>
<%=Html.ActionLink("這是一個串連", "Index" , new { page = 1 }, new { id = "link1" })%>

產生結果為:

複製代碼 代碼如下:

<a href="/">這是一個串連</a>

帶有QueryString的寫法

複製代碼 代碼如下:

<a href="/?page=1">這是一個串連</a>
<a href="/?page=1">這是一個串連</a>

有其它Html屬性的寫法

複製代碼 代碼如下:

<a href="/?Length=4" id="link1">這是一個串連</a>
<a href="/" id="link1">這是一個串連</a>

QueryString與Html屬性同時存在

複製代碼 代碼如下:

<a href="/?page=1" id="link1">這是一個串連</a>
<a href="/?page=1" id="link1">這是一個串連</a>

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 2種方法

複製代碼 代碼如下:

<%using(Html.BeginForm("index","home",FormMethod.Post)){%>
<%} %>

複製代碼 代碼如下:

<%Html.BeginForm("index", "home", FormMethod.Post);//注意這裡沒有=輸出%>
<%Html.EndForm(); %>

產生結果:

複製代碼 代碼如下:

<form action="/home/index" method="post"></form>

4.TextBox

複製代碼 代碼如下:

<%=Html.TextBox("input1") %>
<%=Html.TextBox("input2",Model.CategoryName,new{ @style = "width:300px;" }) %>
<%=Html.TextBox("input3", ViewData["Name"],new{ @style = "width:300px;" }) %>
<%=Html.TextBoxFor(a => a.CategoryName, new { @style = "width:300px;" })%>

產生結果:

複製代碼 代碼如下:

<input id="input1" name="input1" type="text" value="" />
<input id="input2" name="input2" style="width:300px;" type="text" value="Beverages" />
<input id="input3" name="input3" style="width:300px;" type="text" value="" />
<input id="CategoryName" name="CategoryName" style="width:300px;" type="text" value="Beverages" />

5.TextArea

複製代碼 代碼如下:

<%=Html.TextArea("input5", Model.CategoryName, 3, 9,null)%>
<%=Html.TextAreaFor(a => a.CategoryName, 3, 3, null)%>

產生結果:

複製代碼 代碼如下:

<textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea>
<textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea>

6.CheckBox

複製代碼 代碼如下:

<%=Html.CheckBox("chk1",true) %>
<%=Html.CheckBox("chk1", new { @class="checkBox"}) %>
<%=Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })%>

產生結果:

複製代碼 代碼如下:

<input checked="checked" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
<input class="checkBox" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
<input checked="checked" class="checkBox" id="IsVaild" name="IsVaild" type="checkbox" value="true" /><input name="IsVaild" type="hidden" value="false" />

7.ListBox

複製代碼 代碼如下:

<%=Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])%>
<%=Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])%>

產生結果:

複製代碼 代碼如下:

<select id="lstBox1" multiple="multiple" name="lstBox1">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select id="CategoryName" multiple="multiple" name="CategoryName">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>

8.DropDownList

複製代碼 代碼如下:

<%= Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--")%>
<%=Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })%>

產生結果:

複製代碼 代碼如下:

<select id="ddl1" name="ddl1">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select class="dropdownlist" id="CategoryName" name="CategoryName">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>

9.Partial 視圖模板

webform裡叫自訂控制項。功能都是為了複用。但使用上自訂控制項真的很難用好。

複製代碼 代碼如下:

<% Html.RenderPartial("DinnerForm"); %>

看清楚了沒有等號的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.