【MVC】ASP.NET MVC HtmlHelper用法大全

來源:互聯網
上載者:User

標籤:

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 , Hidden
<%=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>
</select>
<select id="CategoryName" multiple="multiple" name="CategoryName">
<option value="1">Beverages</option>
<option value="2">Condiments</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> 
</select> 
<select class="dropdownlist" id="CategoryName" name="CategoryName">
<option value="">--Select One--</option> 
<option value="1">Beverages</option> 
<option value="2">Condiments</option> 
</select>
 
本文轉自:http://www.cnblogs.com/yuanhuaming/archive/2010/02/18/1669184.html

【MVC】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.