脚本宝典收集整理的这篇文章主要介绍了ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
HtMLHelPEr类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是Formextensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,RenderPartialExtensions类。
为了方便开发者使用HtmlHelper控件,在视图ViewPage类中设置了一个属性Html它就是HtmlHelper类型。
一.FormExtensions类
定义了3中类型的扩展方法BeginForm,BeginRouteForm,EnDForm。
(1) BeginForm (实现表单定义的开始部分)
重载方法有13个:
BeginForm();
BeginForm(Object routeValues);
BeginForm(RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName);
BeginForm(string actionName,string controllerName,object routeValues);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName,FormMethod method);
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeVaues,FormMethod method);
BeginForm(string actionName,string controllerName,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,FormMethod method,IDictionary<string,object> htmlAttributes);
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FormMethod method,IDictionary<string,object> htmlAttributes);
对于第二个重载方法可以设置如下:
在上述代码中,设置了路由值的一个实例化对象,输出的HTML语句是:
对于最后一个第十三个方法的最后一个参数是实例化对象设置相关属性的值例如class,width等。
(2)BeginRouteForm (主要实现表单定义的开始部分,以路由的方法设置action的值)
有12个重载方法:
BeginRouteForm(object routeValues);
BeginRouteForm(RouteValueDictionary routeValues);
BeginRouteForm(string routeName);
BeginRouteForm(string routeName,object routeValues);
BeginRouteForm(string routeName,RouteValueDictionary routeValues);
BeginRouteForm(string routeName,FormMethod method);
BeginRouteForm(string routeName,object routeValues,FormMethod method);
……
对于第一个重载方法:
BeginForm与BeginRouteForm的区别就在于第一个的action是action第二个的action是Home/action
(3)EndForm(实现表单的定义的结束部分)
相当于</Form>
二.InputExtensions类有5种类型的扩展方法,可在视图中设置checkBox,hidden,password,radioButton,textBox控件。
(1)CheckBox 实现复选框控件有6个重载方法
CheckBox(string name);
CheckBox(string name,bool isChecked);
CheckBox(string name,bool isChecked,object htmlAttributes);
CheckBox(string name,object htmlAttributes);
CheckBox(string name,Idictionary<string,object> htmlAttributes);
CheckBox(string name,bool isChecked,Idictionary<string,object> htmlAttributes);
设置复选框的实现代码:
运行上述代码,上述复选框的设置代码对应的HTML语句:
在后台检索checkBox
(2)Hidden 表单中的隐藏数值,有4个重载方法。
Hidden(string name);
Hidden(string name,object value);
Hidden(string name,object value,object htmlAttributes);
Hidden(string name,object value,Idictionary<string,object> htmlAttributes);
eg:
对应输出的Html语句如下:
(3)Password 主要是输入密码的文本框,有4个重载方法。
Hidden(string name);
Password (string name,object value);
Password (string name,object value,object htmlAttributes);
Password (string name,object value,Idictionary<string,object> htmlAttributes);
eg:
--------------------------------------------------------------------------------------------
HTML扩展类的所有方法都有2个参数:
以textbox为例子
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary<string, Object> htmlAttributes )
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object htmlAttributes )
这2个参数代表这个html标签的属性集合。使用方法如下。
1.ActionLink
带有QueryString的写法
有其它Html属性的写法
QueryString与Html属性同时存在
生成结果为:
带有QueryString的写法
有其它Html属性的写法
QueryString与Html属性同时存在
2.RouteLink
跟ActionLink在功能上一样。
带QueryString
生成结果:
3.Form 2种方法
生成结果:
4.TextBox
生成结果:
5.TextArea
生成结果:
6.CheckBox
生成结果:
7.ListBox
生成结果:
8.DropDownList
生成结果:
9.Partial 视图模板
webform里叫自定义控件。功能都是为了复用。但使用上自定义控件真的很难用好。
看清楚了没有等号的。
以上是脚本宝典为你收集整理的ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解全部内容,希望文章能够帮你解决ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。