MVC HtmlHelper用法大全
1. ActionLink - 链接到操作方法。
2. BeginForm * - 标记窗体的开头并链接到呈现该窗体的操作方法。
3. CheckBox * - 呈现复选框。
4. DropDownList * - 呈现下拉列表。
5. Hidden - 在窗体中嵌入未呈现的信息以供用户查看。
6. ListBox * - 呈现列表框。
7. Password - 呈现用于输入密码的文本框。
8. RadioButton * - 呈现单选按钮。
9. TextArea - 呈现文本区域(多行文本框)。
10. TextBox * - 呈现文本框
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" })
生成结果为:
2).RouteLink
//跟ActionLink在功能上一样。 @Html.RouteLink("关于", "about", new { }) //带QueryString @Html.RouteLink("关于", "about", new { page = 1 }) @Html.RouteLink("关于", "about", new { page = 1 }, new { id = "link1" })
生成结果:
3).Form 3种方法
@using(Html.BeginForm("index","home",FormMethod.Post)){ }
@Html.BeginForm("index", "home", FormMethod.Post)
@Html.BeginForm("方法名", "home", FormMethod.Post)
Html.GeginForm是直接提交本地
生成结果:
<form action="/home/index" method="post"></form>
4).TextBox , Hidden
@Html.TextBox("input1") @Html.TextBox("input2",Model.CategoryName,new{ @style = "300px;" }) @Html.TextBox("input3", ViewData["Name"],new{ @style = "300px;" }) @Html.TextBoxFor(a => a.CategoryName, new { @style = "300px;" })
生成结果:
5).TextArea
@Html.TextArea("input5", Model.CategoryName, 3, 9,null) @Html.TextAreaFor(a => a.CategoryName, 3, 3, null)
生成结果:
6).CheckBox
@Html.CheckBox("chk1",true) @Html.CheckBox("chk1", new { @class="checkBox"}) @Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })
生成结果:
7).ListBox
@Html.ListBox("lstBox1",(SelectList)ViewData["Categories"]) @Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])
生成结果:
8).DropDownList
@ Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--") @Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })
生成结果:
9).Partial 视图模板
类似于webform里的自定义控件。
@Html.RenderPartial("DinnerForm")