ASP.NET MVC5总结(一)@HTML和对应的HTML

HtmlHelper用来在视图中呈现 HTML 控件,主要分为以下几类:

1.ActionLink - 链接到操作方法

 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" })

对应的HTML

 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" })

对应的HTML

<a href="/about">关于</a>
<a href="/about?page=1">关于</a>
<a href="/about?page=1" id="link1">关于</a>

2.BeginForm - 标记窗体的开头并链接到呈现该窗体的操作方法

1 @using(Html.BeginForm("index","home",FormMethod.Post)){
2 
3 }
4 Or
5 @Html.BeginForm("index", "home", FormMethod.Post)
6 @Html.EndForm()

对应的HTML

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

3.CheckBox  - 呈现复选框

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

对应的HTML

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  - 呈现下拉列表

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

对应的HTML

 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>
posted @ 2016-08-07 15:53  garsonguo  阅读(3589)  评论(0编辑  收藏  举报