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.BeginForm(new{action="action",controller="actroller",id="2"});
</div>
在上述代码中,设置了路由值的一个实例化对象,输出的HTML语句是:
<form action="actroller/action/2" method="post"/>
</div>
对于最后一个第十三个方法的最后一个参数是实例化对象设置相关属性的值例如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);
……
对于第一个重载方法:
Html.BeginRouteForm(new {action="action"});
</div>
<form action="Home/action" method="post"/>Home是页面所在的目录
</div>
BeginForm与BeginRouteForm的区别就在于第一个的action是action第二个的action是Home/action
(3)EndForm(实现表单的定义的结束部分)
Html.EndForm();
</div>
相当于</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.BeginForm("CheckBox","Home") %>
<fieldset>
<legend>设置字体:</lengend>
<%=Html.CheckBox("MyCheckBox1",true,new{id="checkBox1"})%>
<label for="checkBox1">黑体</label>
<%=Html.CheckBox("MyCheckBox2",false,new{id="checkBox2"})%>
<label for="checkBox1">斜体</label>
<br/><br/>
<input type="submit" value="Submit"/>
</fieldset>
<%Html.EndForm();%>
</div>
运行上述代码,上述复选框的设置代码对应的HTML语句:
<input checked="checked" id="checkBox1" name="MyCheckBox1" type="CheckBox" value="true"/>
<input name="MyCheckBox1" type="hidden" value="false"/>
<input id="checkBox2" name="MyCheckBox2" type="CheckBox" value="false"/>
<input name="MyCheckBox2" type="hidden" value="false"/>
</div>
在后台检索checkBox
public ActionResult CheckBox (FormCollection formCollection)
{
bool MyCheckBox1=formCollection[0].Contains("true");//检索第一个复选框是否被选中
bool MyCheckBox2=formCollection["MyCheckBox2"].Contains("true");//检索名字是MyCheckBox2的复选框是否倍选中
ViewData["CheckBox1"]=MyCheckBox1;
ViewData["CheckBox2"]=MyCheckBox2;
return View();
}
</div>
(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.Hidden("testName");
</div>
对应输出的Html语句如下:
<input id="testName" name="testName" type="hidden" value=""/>
</div>
(
您可能想查找下面的文章:
- 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定
- ASP.NET MVC从视图传参到控制器的几种形式
- ASP.NET MVC 4 中的JSON数据交互的方法
- ASP.NET MVC制作404跳转实例(非302和200)
- 详解ASP.NET MVC 利用Razor引擎生成静态页
- 详解ASP.NET MVC 解析模板生成静态页(RazorEngine)
- ASP.NET MVC4 利用uploadify.js多文件上传
- ASP.NET mvc4中的过滤器的使用
- Asp.net MVC下使用Bundle合并、压缩js与css文件详解
- 详解Asp.Net MVC——控制器与动作(Controller And Action)