HtmlHelper用來在視圖中呈現 HTML 控件,主要分爲如下幾類:post
1.ActionLink - 連接到操做方法ui
1 @Html.ActionLink("這是一個鏈接", "Index", "Home") 2 帶有QueryString的寫法 3 @Html.ActionLink("這是一個鏈接", "Index", "Home", new { page=1 },null) 4 @Html.ActionLink("這是一個鏈接", "Index", new { page=1 }) 5 有其它Html屬性的寫法 6 @Html.ActionLink("這是一個鏈接", "Index", "Home", new { id="link1" }) 7 @Html.ActionLink("這是一個鏈接", "Index",null, new { id="link1" }) 8 QueryString與Html屬性同時存在 9 @Html.ActionLink("這是一個鏈接", "Index", "Home", new { page = 1 }, new { id = "link1" }) 10 @Html.ActionLink("這是一個鏈接", "Index" , new { page = 1 }, new { id = "link1" })
對應的HTMLspa
1 <a href="/">這是一個鏈接</a> 2 帶有QueryString的寫法 3 <a href="/?page=1">這是一個鏈接</a> 4 <a href="/?page=1">這是一個鏈接</a> 5 有其它Html屬性的寫法 6 <a href="/?Length=4" id="link1">這是一個鏈接</a> 7 <a href="/" id="link1">這是一個鏈接</a> 8 QueryString與Html屬性同時存在 9 <a href="/?page=1" id="link1">這是一個鏈接</a> 10 <a href="/?page=1" id="link1">這是一個鏈接</a>
RoutLink
@Html.RouteLink("關於", "about", new { }) 帶QueryString @Html.RouteLink("關於", "about", new { page = 1 }) @Html.RouteLink("關於", "about", new { page = 1 }, new { id = "link1" })
對應的HTMLcode
<a href="/about">關於</a> <a href="/about?page=1">關於</a> <a href="/about?page=1" id="link1">關於</a>
2.BeginForm - 標記窗體的開頭並連接到呈現該窗體的操做方法orm
1 @using(Html.BeginForm("index","home",FormMethod.Post)){ 2 3 } 4 Or 5 @Html.BeginForm("index", "home", FormMethod.Post) 6 @Html.EndForm()
對應的HTMLblog
<form action="/home/index" method="post"></form>
3.CheckBox - 呈現複選框ip
1 @Html.CheckBox("chk1",true) 2 @Html.CheckBox("chk1", new { @class="checkBox"}) 3 @Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })
對應的HTMLpdo
1 <input checked="checked" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" /> 2 <input class="checkBox" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" /> 3 <input checked="checked" class="checkBox" id="IsVaild" name="IsVaild" type="checkbox" value="true" /><input name="IsVaild" type="hidden" value="false" />
4.DropDownList - 呈現下拉列表input
@Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--") @Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })
對應的HTMLio
1 <select id="ddl1" name="ddl1"> 2 <option value="">--Select One--</option> 3 <option value="1">Beverages</option> 4 <option value="2">Condiments</option> 5 <option selected="selected" value="3">Confections</option> 6 <option value="4">Dairy Products</option> 7 <option value="5">Grains/Cereals</option> 8 <option value="6">Meat/Poultry</option> 9 <option value="7">Produce</option> 10 <option value="8">Seafood</option> 11 </select> 12 <select class="dropdownlist" id="CategoryName" name="CategoryName"> 13 <option value="">--Select One--</option> 14 <option value="1">Beverages</option> 15 <option value="2">Condiments</option> 16 <option value="3">Confections</option> 17 <option value="4">Dairy Products</option> 18 <option value="5">Grains/Cereals</option> 19 <option value="6">Meat/Poultry</option> 20 <option value="7">Produce</option> 21 <option value="8">Seafood</option> 22 </select>
5.ListBox - 呈現列表框
1 @Html.ListBox("lstBox1",(SelectList)ViewData["Categories"]) 2 @Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])
對應的HTML
1 <select id="lstBox1" multiple="multiple" name="lstBox1"> 2 <option value="1">Beverages</option> 3 <option value="2">Condiments</option> 4 <option selected="selected" value="3">Confections</option> 5 <option value="4">Dairy Products</option> 6 <option value="5">Grains/Cereals</option> 7 <option value="6">Meat/Poultry</option> 8 <option value="7">Produce</option> 9 <option value="8">Seafood</option> 10 </select> 11 <select id="CategoryName" multiple="multiple" name="CategoryName"> 12 <option value="1">Beverages</option> 13 <option value="2">Condiments</option> 14 <option value="3">Confections</option> 15 <option value="4">Dairy Products</option> 16 <option value="5">Grains/Cereals</option> 17 <option value="6">Meat/Poultry</option> 18 <option value="7">Produce</option> 19 <option value="8">Seafood</option> 20 </select>
6.Password - 呈現用於輸入密碼的文本框
@Html.PasswordFor(m => m.Password, new { @class = "form-control input_width", @placeholder = "請輸入密碼" })
對應的HTML
<input class="form-control input_width" data-val="true" data-val-required="請輸入密碼" id="Password" name="Password" placeholder="請輸入密碼" type="password">
7.RadioButton - 呈現單選按鈕
@Html.RadioButtonFor(m => m.Gender, "Male") @Html.RadioButtonFor(m => m.Gender, "Female")
對應的HTML
<input type="radio" name="radiobutton" value="radiobutton" > Male <input type="radio" name="radiobutton" value="radiobutton" checked> Female
8.TextArea - 呈現文本區域(多行文本框)
@Html.TextArea("input5", Model.CategoryName, 3, 9,null) @Html.TextAreaFor(a => a.CategoryName, 3, 3, null)
對應的HTML
<textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea> <textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea>
9.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;" })
對應的HTML
<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" />
10.Partial視圖RenderPartial(直接將用戶控件嵌到該界面)
@Html.RenderPartial("DinnerForm")
@{ Html.RenderPartial("~/Areas/Comm/Views/Shared/LogOnUserControl.ascx"); }
11.Label
@Html.Label("GenreId")
對應的HTML
<label for="GenreId">Genre</label>
12.Url.Content
<script src="@Url.Content("~/Scripts/Jquery-1.10.1.min.js")" type="text/javescript"></script>