HtmlHelper用来在视图中呈现 HTML 控件,主要分为以下几类:
1.ActionLink - 链接到操作方法
@Html.ActionLink("这是一个连接", "Index", "Home")
带有QueryString的写法
@Html.ActionLink("这是一个连接", "Index", "Home", new { page= },null)
@Html.ActionLink("这是一个连接", "Index", new { page= })
有其它Html属性的写法
@Html.ActionLink("这是一个连接", "Index", "Home", new { id="link1" })
@Html.ActionLink("这是一个连接", "Index",null, new { id="link1" })
QueryString与Html属性同时存在
@Html.ActionLink("这是一个连接", "Index", "Home", new { page = }, new { id = "link1" })
@Html.ActionLink("这是一个连接", "Index" , new { page = }, new { id = "link1" })
对应的HTML
<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>
RoutLink
@Html.RouteLink("关于", "about", new { })
带QueryString
@Html.RouteLink("关于", "about", new { page = })
@Html.RouteLink("关于", "about", new { page = }, new { id = "link1" })
对应的HTML
<a href="/about">关于</a>
<a href="/about?page=1">关于</a>
<a href="/about?page=1" id="link1">关于</a>
2.BeginForm - 标记窗体的开头并链接到呈现该窗体的操作方法
@using(Html.BeginForm("index","home",FormMethod.Post)){ }
Or
@Html.BeginForm("index", "home", FormMethod.Post)
@Html.EndForm()
对应的HTML
<form action="/home/index" method="post"></form>
3.CheckBox - 呈现复选框
@Html.CheckBox("chk1",true)
@Html.CheckBox("chk1", new { @class="checkBox"})
@Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })
对应的HTML
<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" />
4.DropDownList - 呈现下拉列表
@Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--")
@Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })
对应的HTML
<select id="ddl1" name="ddl1">
<option value="">--Select One--</option>
<option value="">Beverages</option>
<option value="">Condiments</option>
<option selected="selected" value="">Confections</option>
<option value="">Dairy Products</option>
<option value="">Grains/Cereals</option>
<option value="">Meat/Poultry</option>
<option value="">Produce</option>
<option value="">Seafood</option>
</select>
<select class="dropdownlist" id="CategoryName" name="CategoryName">
<option value="">--Select One--</option>
<option value="">Beverages</option>
<option value="">Condiments</option>
<option value="">Confections</option>
<option value="">Dairy Products</option>
<option value="">Grains/Cereals</option>
<option value="">Meat/Poultry</option>
<option value="">Produce</option>
<option value="">Seafood</option>
</select>
5.ListBox - 呈现列表框
@Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])
@Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])
对应的HTML
<select id="lstBox1" multiple="multiple" name="lstBox1">
<option value="">Beverages</option>
<option value="">Condiments</option>
<option selected="selected" value="">Confections</option>
<option value="">Dairy Products</option>
<option value="">Grains/Cereals</option>
<option value="">Meat/Poultry</option>
<option value="">Produce</option>
<option value="">Seafood</option>
</select>
<select id="CategoryName" multiple="multiple" name="CategoryName">
<option value="">Beverages</option>
<option value="">Condiments</option>
<option value="">Confections</option>
<option value="">Dairy Products</option>
<option value="">Grains/Cereals</option>
<option value="">Meat/Poultry</option>
<option value="">Produce</option>
<option value="">Seafood</option>
</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, , ,null)
@Html.TextAreaFor(a => a.CategoryName, , , null)
对应的HTML
<textarea cols="" id="input5" name="input5" rows="">Beverages</textarea>
<textarea cols="" id="CategoryName" name="CategoryName" rows="">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..min.js")" type="text/javescript"></script>