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 这是一个连接 2 带有QueryString的写法 3 这是一个连接 4 这是一个连接 5 有其它Html属性的写法 6 这是一个连接 7 这是一个连接 8 QueryString与Html属性同时存在 9 这是一个连接10 这是一个连接
RoutLink @Html.RouteLink("关于", "about", new { })带QueryString@Html.RouteLink("关于", "about", new { page = 1 })@Html.RouteLink("关于", "about", new { page = 1 }, new { id = "link1" })
对应的HTML
2.BeginForm - 标记窗体的开头并链接到呈现该窗体的操作方法
1 @using(Html.BeginForm("index","home",FormMethod.Post)){2 3 }4 Or5 @Html.BeginForm("index", "home", FormMethod.Post)6 @Html.EndForm()
对应的HTML
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 2 3
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 12
5.ListBox - 呈现列表框
1 @Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])2 @Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])
对应的HTML
1 11
6.Password - 呈现用于输入密码的文本框
@Html.PasswordFor(m => m.Password, new { @class = "form-control input_width", @placeholder = "请输入密码" })
对应的HTML
7.RadioButton - 呈现单选按钮
@Html.RadioButtonFor(m => m.Gender, "Male")@Html.RadioButtonFor(m => m.Gender, "Female")
对应的HTML
Male Female
8.TextArea - 呈现文本区域(多行文本框)
@Html.TextArea("input5", Model.CategoryName, 3, 9,null)@Html.TextAreaFor(a => a.CategoryName, 3, 3, null)
对应的HTML
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
10.Partial视图RenderPartial(直接将用户控件嵌到该界面)
@Html.RenderPartial("DinnerForm")
@{ Html.RenderPartial("~/Areas/Comm/Views/Shared/LogOnUserControl.ascx"); }
11.Label
@Html.Label("GenreId")
对应的HTML
12.Url.Content