MVC HTML 帮助器详解(一)

一、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()

使用方法

  1. @Html.Action(string actionName)                                                                                                                                                   说明:调用指定子操作方法并以HTML字符串形式返回结果                                                                                                       示例: @Html.Action("delete");
  2. @Html.Action(string actionName,object routeValues)                                                                                                                  说明:使用指定参数调用指定子操作方法,并以HTML字符串形式返回结果                                                                            示例:  @Html.Action("delete",new { id=1}); 
  3. @Html.Action(string actionName,RouteVaueDictionary routeValues)                                                                                        说明:使用指定参数调用指定子操作方法,并以HTML字符串形式返回结果                                                                             示例: @Html.Action("add",new RouteValueDictionary { { "id",1},{ "username", "test" } });
  4. @Html.Action(string actionName,string controllerName)                                                                                                          说明:使用指定控制器名称调用指定子操作方法,并以HTML字符串形式返回结果                                                                 示例:@Html.Action("delete","UserInfo");
  5. @Html.Action(string actionName,string controllerName,object routeValues)                                                                         说明:使用指定参数和控制器名称调用指定子操作方法,并以HTML字符串形式返回结果                                                    示例:@Html.Action("delete", "UserInfo", new { id = 1 });
  6. @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()

使用方法

  1. @Html.ActionLink(string linkText,string actionName)                                                                                                                   说明:返回指定的链接文本和操作的定位点元素(a元素)                                                                                                       示例:@Html.ActionLink("删除", "delete")
  2. @Html.ActionLink(string linkText,string actionName,string controllerName)                                                                          说明:返回指定的链接文本、操作和控制器的定位点元素(a元素)                                                                                              示例:@Html.ActionLink("列表", "getlist", "UserInfo")
  3. @Html.ActionLink(string linkText,string actionName,object routeValues)                                                                             说明:返回指定的链接文本、操作和路由值的定位点元素(a元素)                                                                                          示例:@Html.ActionLink("删除", "delete", new { id = 1 })
  4. @Html.ActionLink(string linkText,string actionName,RouteVaueDictionary routeValues)                                                     说明:返回指定的链接文本、操作和作为路由值字典的路由值的定位点元素(a元素)                                                          示例: @Html.ActionLink("新增","add", new RouteValueDictionary { { "id", 1 }, { "username", "test" } })
  5. @Html.ActionLink(string linkText,string actionName,object routeValues,object htmlAttributes)                                         说明:返回指定的链接文本、操作、路由值和HTML特性的定位点元素(a元素)                                                                     示例:@Html.ActionLink("删除","delete",new { id=1},new {@class="btnLink",id="test"})
  6. @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"} })
  7. @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" })
  8. @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" } })
  9. @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>
  10. @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()

使用方法

  1. @Html.AntiForgeryToken() 

 同时在对应的Controller中添加 [ValidateAntiForgeryToken] 标签,此方法用于防止跨站请求伪造(CSRF(Cross-site request forgery)),用于Form提交表单中

4、AttributeEncode()

使用方法 

  1.  @Html.AttributeEncode(string value)                                                                                                                                                说明:将指定的特性字符串转换为 HTML 编码的字符串 
  2. @Html.AttributeEncode(object value)                                                                                                                                                 说明:将指定的特性对象转换为 HTML 编码的字符串

可参考:https://msdn.microsoft.com/zh-cn/library/system.web.mvc.htmlhelper.attributeencode(v=vs.98).aspx 

 5、BeginForm()

使用方法

用于Form表单中

  1. @Html.BeginFrom()                                                                                                                                                                        说明:将<form>开始标记写入响应,表单使用POST方法,并由视图的操作方法处理请求
  2. @Html.BeiginForm(object routeValues)                                                                                                                                        说明:将<form>开始标记写入响应,并在操作特性中包含路由值,表单使用POST方法,并由视图的操作方法处理请求 示例:@Html.BeginForm(new { id = 1 })
  3. @Html.BeiginForm(RouteValueDictionary routeValues)                                                                                                                 说明:将<form>开始标记写入响应,并在操作特性中包含作为路由值字典的路由值,表单使用POST方法,并由视图的操作方法处理请求                                                                                                                                                                           示例:@Html.BeginForm(new RouteValueDictionary { { "id",1},{ "username","test"} })
  4. @Html.BeiginForm(string actionName,string controllerName)                                                                                                 说明: 将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用POST方法                           示例:@Html.BeginForm("GetList","UserInfo")
  5. @Html.BeiginForm(string actionName,string controllerName,FromMethod method)                                                             说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器和操作,表单使用指定的Http方法                   示例:@Html.BeginForm("GetList","UserInfo",FormMethod.Get)
  6. @Html.BeiginForm(string actionName,string controllerName,object routeValues)                                                                说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值,表单使用POST方法           示例:@Html.BeginForm("GetModel","UserInfo", new { id = 1 })
  7. @Html.BeiginForm(string actionName,string controllerName,RouteValueDictionaryrouteValues)                                           说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和作为路由值字典的路由值,表单使用POST方法                                                                                                                                                                                        示例:@Html.BeginForm("GetModel","UserInfo", new RouteValueDictionary { { "id",1} })
  8. @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"})
  9. @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" } })
  10. @Html.BeiginForm(string actionName,string controllerName,object routeValues,FromMethod method)                           说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由值,表单使用指定的Http方法  示例:@Html.BeginForm("GetModel", "UserInfo", new { id=1},FormMethod.Get)
  11. @Html.BeiginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FromMethod method)说明:将<form>开始标记写入响应,并将操作标记设置为指定的控制器、操作和路由字典中的路由值,表单使用指定的Http方法                                                                                                                                                                                       示例:@Html.BeginForm("GetModel", "UserInfo", new RouteValueDictionary { { "id", 1 } }, FormMethod.Get)
  12. @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" })
  13. @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>开始标记写入响应,在用户提交窗体时,将由路由目标处理该请求 

  1.  @Html.BeginRouteForm(object routeValues)                                                                                                                                示例:@Html.BeginRouteForm(new { id = 1 })
  2. @Html.BeginRouteForm(RouteValueDictionary routeValues)                                                                                                        示例: @Html.BeginRouteForm(new RouteValueDictionary { { "id",1} })
  3. @Html.BeginRouteForm(string routeName)                                                                                                                                 示例:@Html.BeginRouteForm("myRoute")
  4. @Html.BeginRouteForm(string routeName,FormMethod method)                                                                                           示例:@Html.BeginRouteForm("myRoute",FormMethod.Get)
  5. @Html.BeginRouteForm(string routeName,object routeValues)                                                                                               示例:@Html.BeginRouteForm("myRoute",new { id=1})
  6. @Html.BeginRouteForm(string routeName,RouteValueDictionary routeValues)                                                                      示例:@Html.BeginRouteForm("MyRoute",new RouteValueDictionary { { "id",1} })
  7. @Html.BeginRouteForm(string routeName,FormMethod method,object htmlAttributes)                                                      示例:@Html.BeginRouteForm("myRoute", FormMethod.Get,new { @class= "form-group", id="form1"})
  8. @Html.BeginRouteForm(string routeName,FormMethod method,IDictionary<string,object> htmlAttributes)                     示例:@Html.BeginRouteForm("myRoute", FormMethod.Get, new Dictionary<string, object>() { { "class", "form-group" }, { "id", "form1" } })
  9. @Html.BeginRouteForm(string routeName,object routeValues,FormMethod method)                                                         示例:@Html.BeginRouteForm("myRoute", new { id=1},FormMethod.Get)
  10. @Html.BeginRouteForm(string routeName,RouteValueDictionary routeValues,FormMethod method)                              示例:@Html.BeginRouteForm("myRoute", new RouteValueDictionary{ {"id",1 } }, FormMethod.Get)
  11. @Html.BeginRouteForm(string routeName,object routeValues,FormMethod method,object htmlAttributes)                     示例:@Html.BeginRouteForm("myRoute",new { id=1},FormMethod.Get, new { @class = "form-group", id = "form1" })
  12. @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()

使用方法

  1. @Html.ChekBox(string name)                                                                                                                                                       说明:设置checkbox的name属性,返回复选框input元素                                                                                                       示例:@Html.CheckBox("ck1")
  2. @Html.ChekBox(string name,bool isChecked)                                                                                                                            说明:设置checkbox的name属性以及设置该复选框是否选中,返回复选框input元素                                                            示例:@Html.CheckBox("ck2",true)
  3. @Html.ChekBox(string name,IDictionary<string,object> htmlAttributes)                                                                                        说明:设置checkbox的name属性以及设置该复选框的其他属性                                                                                           示例: @Html.CheckBox("ck3",new Dictionary<string, object> { { "id","ck3"} })
  4. @Html.ChekBox(string name,object htmlAttributes)                                                                                                                   说明:设置checkbox的name属性以及设置该复选框的其他属性                                                                                           示例: @Html.CheckBox("ck4",new {id="ck4"})
  5. @Html.ChekBox(string name,bool isChecked,object htmlAttributes)                                                                                       说明:设置checkbox的name属性、设置该复选框是否选中以及设置该复选框的其他属性                                                 示例:@Html.CheckBox("ck5",true, new { id = "ck4" })
  6. @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()

使用方法

  1. @Html.CheckBoxFor(System.Linq.Expressions.Expression<Func<T,bool>> expression)                                                   说明:使用表达式为checkbox设置是否选中                                                                                                                             示例: @Html.CheckBoxFor(item=>item.isDelete)  //实体类Item的字段值 isDelete,赋值后,默认checkbox的id和name属性值为isDelete,如果 isDelete为true,则复选框选中
  2. @Html.CheckBoxFor(System.Linq.Expressions.Expression<Func<T,bool>> expression,object htmlAttributes)                  说明:使用表达式为checkbox设置是否选中,设置属性值                                                                                                      示例: @Html.CheckBoxFor(item=>item.isDelete,new{@class="checkbox"})
  3. @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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值