一、HTML帮助器简单介绍
HTML 帮助器用于修改 HTML 输出。
通过 MVC,HTML 帮助器类似于传统的 ASP.NET Web Form 控件。
类似 ASP.NET 中的 web form 控件,HTML 帮助器用于修改 HTML。但是 HTML 帮助器更轻。与 web form 控件不同,HTML 帮助器没有事件模型和 view state。
在大多数情况下,HTML 帮助器仅仅是返回字符串的方法。
通过 MVC,您能够创建自己的帮助器,或者使用内建的 HTML 帮助器。
二、帮助器详解
1、Action()
使用方法
- @Html.Action(string actionName) 说明:调用指定子操作方法并以HTML字符串形式返回结果 示例: @Html.Action("delete");
- @Html.Action(string actionName,object routeValues) 说明:使用指定参数调用指定子操作方法,并以HTML字符串形式返回结果 示例: @Html.Action("delete",new { id=1});
- @Html.Action(string actionName,RouteVaueDictionary routeValues) 说明:使用指定参数调用指定子操作方法,并以HTML字符串形式返回结果 示例: @Html.Action("add",new RouteValueDictionary { { "id",1},{ "username", "test" } });
- @Html.Action(string actionName,string controllerName) 说明:使用指定控制器名称调用指定子操作方法,并以HTML字符串形式返回结果 示例:@Html.Action("delete","UserInfo");
- @Html.Action(string actionName,string controllerName,object routeValues) 说明:使用指定参数和控制器名称调用指定子操作方法,并以HTML字符串形式返回结果 示例:@Html.Action("delete", "UserInfo", new { id = 1 });
- @Html.Action(string actionName,string controllerName,RouteVaueDictionary routeValues) 说明:使用指定参数和控制器名称调用指定子操作方法,并以HTML字符串形式返回结果 示例: @Html.Action("delete", "UserInfo", new RouteValueDictionary { { "id", 1 }, { "username", "test" } });
参数说明
参数名 | 描述 |
---|---|
actionName | 指定控制器的方法 |
routeValues | 向对应的action传递的参数 |
controllerName | 指定控制器名称 |
2、ActionLink()
使用方法
- @Html.ActionLink(string linkText,string actionName) 说明:返回指定的链接文本和操作的定位点元素(a元素) 示例:@Html.ActionLink("删除", "delete")
- @Html.ActionLink(string linkText,string actionName,string controllerName) 说明:返回指定的链接文本、操作和控制器的定位点元素(a元素) 示例:@Html.ActionLink("列表", "getlist", "UserInfo")
- @Html.ActionLink(string linkText,string actionName,object routeValues) 说明:返回指定的链接文本、操作和路由值的定位点元素(a元素) 示例:@Html.ActionLink("删除", "delete", new { id = 1 })
- @Html.ActionLink(string linkText,string actionName,RouteVaueDictionary routeValues) 说明:返回指定的链接文本、操作和作为路由值字典的路由值的定位点元素(a元素) 示例: @Html.ActionLink("新增","add", new RouteValueDictionary { { "id", 1 }, { "username", "test" } })
- @Html.ActionLink(string linkText,string actionName,object routeValues,object htmlAttributes) 说明:返回指定的链接文本、操作、路由值和HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("删除","delete",new { id=1},new {@class="btnLink",id="test"})
- @Html.ActionLink(string linkText,string actionName,RouteVaueDictionary routeValues,IDictionary<string,object> htmlAttributes) 说明:返回指定的链接文本、操作、作为路由值字典的路由值和作为字典的HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("新增", "add", new RouteValueDictionary { { "id", 1 }, { "username", "test" } },new Dictionary<string, object>() { {"class","btn-link" },{"id","test1"} })
- @Html.ActionLink(string linkText,string actionName,string controllerName,object routeValues,object htmlAttributes) 说明:返回指定的链接文本、操作、控制器、路由值和HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("删除", "delete","UserInfo", new { id = 1 }, new { @class = "btn-link", id = "test" })
- @Html.ActionLink(string linkText,string actionName,string controllerName,RouteVaueDictionary routeValues,IDictionary<string,object>htmlAttributes) 说明:返回指定的链接文本、操作、控制器、作为路由值字典的路由值和作为字典的HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("新增","add","UserInfo",new RouteValueDictionary { {"id",1 },{ "UserName","test"} }, new Dictionary<string, object>() { { "class", "btn-link" }, { "id", "test1" } })
- @Html.ActionLink(string linkText,string actionName,string controllerName,string protocol,strig hostName,string fragment,object routeValues,object htmlAttributes) 说明:返回指定的链接文本、操作、控制器、协议、主机名、URL片片段、路由值和HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("删除","delete","UserInfo","http","localhost","top",new { id=1},new { @class="btn-link",id="test2"}) 结果:<a href="http://localhost:12120/UserInfo/delete#top?id=1"></a>
- @Html.ActionLink(string linkText,string actionName,string controllerName,string protocol,strig hostName,string fragment,RouteVaueDictionary routeValues,IDictionary<string,object>htmlAttributes) 说明:返回指定的链接文本、操作、控制器、协议、主机名、URL片片段、作为路由值字典的路由值和作为字典的HTML特性的定位点元素(a元素) 示例:@Html.ActionLink("删除", "delete", "UserInfo", "http", "localhost", "top", new RouteValueDictionary { { "id", 1 }}, new Dictionary<string, object>() { { "class", "btn-link" }, { "id", "test2" } })
参数说明
参数名 | 描述 |
---|---|
linkText | 生成的链接所显示的文字 |
actionName | 对应控制器的方法 |
controllerName | 指定控制器的名称 |
protocol | 指定控制器的名称 |
hostName | 指定访问域名 |
fragment | 指定访问锚点 |
routeValues | 向对应的action传递的参数 |
htmlAttributes | 设置<a>标签的属性 |
3、AntiForgeryToken()
使用方法
- @Html.AntiForgeryToken()
同时在对应的Controller中添加 [ValidateAntiForgeryToken] 标签,此方法用于防止跨站请求伪造(CSRF(Cross-site request forgery)),用于Form提交表单中
4、AttributeEncode()
使用方法
- @Html.AttributeEncode(string value) 说明:将指定的特性字符串转换为 HTML 编码的字符串
- @Html.AttributeEncode(object value) 说明:将指定的特性对象转换为 HTML 编码的字符串
可参考:https://msdn.microsoft.com/zh-cn/library/system.web.mvc.htmlhelper.attributeencode(v=vs.98).aspx
5、BeginForm()
使用方法
用于Form表单中
- @Html.BeginFrom() 说明:将<form>开始标记写入响应,表单使用POST方法,并由视图的操作方法处理请求
- @Html.BeiginForm(object routeValues) 说明:将<form>开始标记写入响应,并在操作特性中包含路由值,表单使用POST方法,并由视图的操作方法处理请求 示例:@Html.BeginForm(new { id = 1 })
- @Html.BeiginForm(RouteValueDictionary routeValues) 说明:将<form>开始标记写入响应,并在操作特性中包含作为路由值字典的路由值,表单使用POST方法,并由视图的操作方法处理请求 示例:@Html.BeginForm(new RouteValueDictionary { { "id",1},{ "username","test"} })
- @Html.BeiginForm(string actionName,string controllerName) 说明: 将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用POST方法 示例:@Html.BeginForm("GetList","UserInfo")
- @Html.BeiginForm(string actionName,string controllerName,FromMethod method) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用指定的Http方法 示例:@Html.BeginForm("GetList","UserInfo",FormMethod.Get)
- @Html.BeiginForm(string actionName,string controllerName,object routeValues) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值,表单使用POST方法 示例:@Html.BeginForm("GetModel","UserInfo", new { id = 1 })
- @Html.BeiginForm(string actionName,string controllerName,RouteValueDictionaryrouteValues) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和作为路由值字典的路由值,表单使用POST方法 示例:@Html.BeginForm("GetModel","UserInfo", new RouteValueDictionary { { "id",1} })
- @Html.BeiginForm(string actionName,string controllerName,FromMethod method,object htmlAttributes) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用指定的Http方法 ,并包含HTML特性 示例: @Html.BeginForm("GetList","UserInfo",FormMethod.Get,new { @class="form-group",id="form1"})
- @Html.BeiginForm(string actionName,string controllerName,FromMethod method,IDictionary<string,object> htmlAttributes) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用指定的Http方法 ,并包含字典中的HTML特性 示例:@Html.BeginForm("GetList", "UserInfo", FormMethod.Get,new Dictionary<string, object>() { { "class", "form-group" }, { "id", "form1" } })
- @Html.BeiginForm(string actionName,string controllerName,object routeValues,FromMethod method) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值,表单使用指定的Http方法 示例:@Html.BeginForm("GetModel", "UserInfo", new { id=1},FormMethod.Get)
- @Html.BeiginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FromMethod method)说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由字典中的路由值,表单使用指定的Http方法 示例:@Html.BeginForm("GetModel", "UserInfo", new RouteValueDictionary { { "id", 1 } }, FormMethod.Get)
- @Html.BeiginForm(string actionName,string controllerName,object routeValues,FromMethod method,object htmlAttributes) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值,表单使用指定的Http方法 ,并包含HTML特性 示例:@Html.BeginForm("GetModel", "UserInfo", new { id = 1 }, FormMethod.Get, new { @class = "form-group", id = "form1" })
- @Html.BeiginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FromMethod method,IDictionary<string,object>htmlAttributes) 说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值字典中的路由值,表单使用指定的Http方法 ,并包含字典中的HTML特性 示例:@Html.BeginForm("GetModel", "UserInfo", new RouteValueDictionary { { "id", 1 } }, FormMethod.Get, new Dictionary<string, object>() { { "class", "form-group" }, { "id", "form1" } })
一般使用时,在form表单的结束标记中使用@Html.EndForm()
示例:
<form @Html.BeginForm()>
@{ Html.EndForm()}
</form>
参数说明
参数 | 描述 |
---|---|
actionName | 对应控制器的方法 |
controllerName | 指定控制器的名称 |
method | 指定的Http方法 |
routeValues | 向对应的action传递的参数 |
htmlAttributes | 设置<form>的属性 |
6、BeginRouteForm()
使用方法
用于Form表单中,说明:将<form>开始标记写入响应,在用户提交窗体时,将由路由目标处理该请求
- @Html.BeginRouteForm(object routeValues) 示例:@Html.BeginRouteForm(new { id = 1 })
- @Html.BeginRouteForm(RouteValueDictionary routeValues) 示例: @Html.BeginRouteForm(new RouteValueDictionary { { "id",1} })
- @Html.BeginRouteForm(string routeName) 示例:@Html.BeginRouteForm("myRoute")
- @Html.BeginRouteForm(string routeName,FormMethod method) 示例:@Html.BeginRouteForm("myRoute",FormMethod.Get)
- @Html.BeginRouteForm(string routeName,object routeValues) 示例:@Html.BeginRouteForm("myRoute",new { id=1})
- @Html.BeginRouteForm(string routeName,RouteValueDictionary routeValues) 示例:@Html.BeginRouteForm("MyRoute",new RouteValueDictionary { { "id",1} })
- @Html.BeginRouteForm(string routeName,FormMethod method,object htmlAttributes) 示例:@Html.BeginRouteForm("myRoute", FormMethod.Get,new { @class= "form-group", id="form1"})
- @Html.BeginRouteForm(string routeName,FormMethod method,IDictionary<string,object> htmlAttributes) 示例:@Html.BeginRouteForm("myRoute", FormMethod.Get, new Dictionary<string, object>() { { "class", "form-group" }, { "id", "form1" } })
- @Html.BeginRouteForm(string routeName,object routeValues,FormMethod method) 示例:@Html.BeginRouteForm("myRoute", new { id=1},FormMethod.Get)
- @Html.BeginRouteForm(string routeName,RouteValueDictionary routeValues,FormMethod method) 示例:@Html.BeginRouteForm("myRoute", new RouteValueDictionary{ {"id",1 } }, FormMethod.Get)
- @Html.BeginRouteForm(string routeName,object routeValues,FormMethod method,object htmlAttributes) 示例:@Html.BeginRouteForm("myRoute",new { id=1},FormMethod.Get, new { @class = "form-group", id = "form1" })
- @Html.BeginRouteForm(string routeName,RouteValueDictionary routeValues,FormMethod method,IDictionary<string,object>htmlAttributes) 示例:@Html.BeginRouteForm("myRoute", new RouteValueDictionary { { "id", 1 } }, FormMethod.Get, new Dictionary<string, object>() { { "class", "form-group" }, { "id", "form1" } })
参数说明
参数 | 描述 |
---|---|
routeName | 指定的路由名称 |
routeValues | 路由参数 |
method | 指定的Http方法 |
htmlAttributes | 设置<form>的属性 |
7、CheckBox()
使用方法
- @Html.ChekBox(string name) 说明:设置checkbox的name属性,返回复选框input元素 示例:@Html.CheckBox("ck1")
- @Html.ChekBox(string name,bool isChecked) 说明:设置checkbox的name属性以及设置该复选框是否选中,返回复选框input元素 示例:@Html.CheckBox("ck2",true)
- @Html.ChekBox(string name,IDictionary<string,object> htmlAttributes) 说明:设置checkbox的name属性以及设置该复选框的其他属性 示例: @Html.CheckBox("ck3",new Dictionary<string, object> { { "id","ck3"} })
- @Html.ChekBox(string name,object htmlAttributes) 说明:设置checkbox的name属性以及设置该复选框的其他属性 示例: @Html.CheckBox("ck4",new {id="ck4"})
- @Html.ChekBox(string name,bool isChecked,object htmlAttributes) 说明:设置checkbox的name属性、设置该复选框是否选中以及设置该复选框的其他属性 示例:@Html.CheckBox("ck5",true, new { id = "ck4" })
- @Html.ChekBox(string name,bool isChecked,IDictionary<string,object>htmlAttributes) 说明:设置checkbox的name属性、设置该复选框是否选中以及设置该复选框的其他属性 示例:@Html.CheckBox("ck6", true, new Dictionary<string, object> { { "id", "ck6" } })
参数说明
参数 | 描述 |
---|---|
name | 设置name属性值 |
isChcked | 设置是否选中 |
htmlAttributes | 设置checkbox的属性 |
8、CheckBoxFor()
使用方法
- @Html.CheckBoxFor(System.Linq.Expressions.Expression<Func<T,bool>> expression) 说明:使用表达式为checkbox设置是否选中 示例: @Html.CheckBoxFor(item=>item.isDelete) //实体类Item的字段值 isDelete,赋值后,默认checkbox的id和name属性值为isDelete,如果 isDelete为true,则复选框选中
- @Html.CheckBoxFor(System.Linq.Expressions.Expression<Func<T,bool>> expression,object htmlAttributes) 说明:使用表达式为checkbox设置是否选中,设置属性值 示例: @Html.CheckBoxFor(item=>item.isDelete,new{@class="checkbox"})
- @Html.CheckBoxFor(System.Linq.Expressions.Expression<Func<T,bool>> expression,IDictionary<string,object> htmlAttributes) 说明:使用表达式为checkbox设置是否选中,设置属性值 示例: @Html.CheckBoxFor(item=>item.isDelete,new Dictionary<string,object>(){{"class","checkbox"}})
参数说明
参数 | 描述 |
---|---|
expression | 表达式对象 |
htmlAttributes | 设置checkbox的属性 |
推荐参考:https://msdn.microsoft.com/zh-cn/library/system.web.mvc.htmlhelper(v=vs.98).aspx