MVC中HtmlHelper用法大全参考

解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1)
2012-02-27 16:25

HtmlHelper类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是FormExtensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,RenderPartialExtensions类。

为了方便开发者使用HtmlHelper控件,在视图ViewPage类中设置了一个属性Html它就是HtmlHelper类型。

1.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"});

在上述代码中,设置了路由值的一个实例化对象,输出的HTML语句是:

<form action="actroller/action/2" method="post"/>

对于最后一个第十三个方法的最后一个参数是实例化对象设置相关属性的值例如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"});

<form action="Home/action" method="post"/>Home是页面所在的目录

BeginForm与BeginRouteForm的区别就在于第一个的action是action第二个的action是Home/action

(3)EndForm(实现表单的定义的结束部分)

Html.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.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();%>

运行上述代码,上述复选框的设置代码对应的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"/>

在后台检索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();

}

(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");对应输出的Html语句如下:

<input id="testName" name="testName" type="hidden" value=""/>

(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.Password ("MyPwd");对应输出的Html语句如下:

<input id="MyPwd" name="MyPwd" type="password" />

--------------------------------------------------------------------------------------------

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
<%=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" })%>

生成结果为:
<a href="/">这是一个连接</a>
带有QueryString的写法
<a href="/?page=1">这是一个连接</a>
<a href="/?page=1">这是一个连接</a>
有其它Html属性的写法
<a href="/?Length=4" id="link1">这是一个连接</a>
<a href="/" id="link1">这是一个连接</a>
QueryString与Html属性同时存在
<a href="/?page=1" id="link1">这是一个连接</a>
<a href="/?page=1" id="link1">这是一个连接</a>

2.RouteLink
跟ActionLink在功能上一样。
<%=Html.RouteLink("关于", "about", new { })%>
带QueryString
<%=Html.RouteLink("关于", "about", new { page = 1 })%>
<%=Html.RouteLink("关于", "about", new { page = 1 }, new { id = "link1" })%>

生成结果:
<a href="/about">关于</a>
<a href="/about?page=1">关于</a>
<a href="/about?page=1" id="link1">关于</a>
3.Form 2种方法
<%using(Html.BeginForm("index","home",FormMethod.Post)){%>
<%} %>

<%Html.BeginForm("index", "home", FormMethod.Post);//注意这里没有=输出%>
<%Html.EndForm(); %>

生成结果:
<form action="/home/index" method="post"></form>
4.TextBox , Hidden ,
<%=Html.TextBox("input1") %>
<%=Html.TextBox("input2",Model.CategoryName,new{ @style = "width:300px;" }) %>
<%=Html.TextBox("input3", ViewData["Name"],new{ @style = "width:300px;" }) %>
<%=Html.TextBoxFor(a => a.CategoryName, new { @style = "width:300px;" })%>

生成结果:

<input id="input1" name="input1" type="text" value="" />
<input id="input2" name="input2" style="width:300px;" type="text" value="Beverages" />
<input id="input3" name="input3" style="width:300px;" type="text" value="" />
<input id="CategoryName" name="CategoryName" style="width:300px;" type="text" value="Beverages" />

5.TextArea
<%=Html.TextArea("input5", Model.CategoryName, 3, 9,null)%>
<%=Html.TextAreaFor(a => a.CategoryName, 3, 3, null)%>

生成结果:
<textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea>
<textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea>

6.CheckBox
<%=Html.CheckBox("chk1",true) %>
<%=Html.CheckBox("chk1", new { @class="checkBox"}) %>
<%=Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })%>

生成结果:

<input checked="checked" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />

<input class="checkBox" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />

<input checked="checked" class="checkBox" id="IsVaild" name="IsVaild" type="checkbox" value="true" /><input name="IsVaild" type="hidden" value="false" />

7.ListBox
<%=Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])%>
<%=Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])%>

生成结果:
<select id="lstBox1" multiple="multiple" name="lstBox1">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select id="CategoryName" multiple="multiple" name="CategoryName">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>

8.DropDownList
<%= Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--")%>
<%=Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })%>

生成结果:
<select id="ddl1" name="ddl1">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option selected="selected" value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<select class="dropdownlist" id="CategoryName" name="CategoryName">
<option value="">--Select One--</option>
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">Meat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>

9.Partial 视图模板
webform里叫自定义控件。功能都是为了复用。但使用上自定义控件真的很难用好。
<% Html.RenderPartial("DinnerForm"); %> 看清楚了没有等号的。

转载于:https://www.cnblogs.com/Tangshao/p/3521512.html

MVC中HtmlHelper用法大全相关推荐

  1. C# ASP.NET MVC HtmlHelper用法大全

    C# ASP.NET MVC HtmlHelper用法大全 (原文) HTML扩展类的所有方法都有2个参数: 以textbox为例子 public static string TextBox( thi ...

  2. MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)

    MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下) 上一节:MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上) 下一节: ...

  3. MVC HtmlHelper用法大全

    http://www.cnblogs.com/jyan/archive/2012/07/23/2604474.html HtmlHelper用来在视图中呈现 HTML 控件. 以下列表显示了当前可用的 ...

  4. ASP.NET中EVAL用法大全

    <%# Bind("Subject") %> //绑定字段 <%# Container.DataItemIndex + 1%> //实现自动编号 <% ...

  5. python中集合用法大全

    目录 序言 1.0 set()函数 2.0 add()函数 3.0 clear()函数 4.0 copy()函数 5.0 discard()函数 6.0 remove()函数 7.0 pop()函数 ...

  6. python中集合用什么表示_python中集合用法大全

    序言: 集合中各元素间是无序的,相同元素在集合中唯一存在.即 集合是无序组合,它没有索引和位置的概念,但可变集合中的元素是可以动态增加或删除的. 集合应用场景: 去重:如列表  去重. 关系测试:判断 ...

  7. Selenium中ExpectedConditions用法大全

    Expected Conditions 用法详解 内容来源于互联网,这里仅做笔记和参考用 1. title_is( String title) 判断当前页面的 title 是否等于预期值 2. tit ...

  8. python中集合用法大全_python中集合的用法

    一.创建集合 1.特点 存放不同的元素 无序 不可变类型(数字.字符串.元组) 2.创建集合 s=set('hello') print(s) s=set(['alex','alex','sb']) p ...

  9. mysql 中alter_MySQL中alter用法大全

    1:删除列 alter table [表名] drop [列名] 2:增加列 alter table [表名] add [列名] int not null comment '注释说明' 3:修改列的类 ...

最新文章

  1. linux neo4j 服务器,如何在linux服务器上配置Neo4j · Digital World
  2. (HDU)1019 --Least Common Multiple(最小公倍数)
  3. 创业产品经理需要懂技术吗?
  4. 需要单机还是集群部署_单机、集群和分布式(微服务结构)的区别
  5. 【属性对比】defer 与 async
  6. 1.文档数据非结构化
  7. 为什么游戏盒子源码那么重要?(不搞清楚游戏盒子源码,游戏代理很难顺利)(远离那些免费游戏盒子源码,会变得不幸)
  8. 搭建 Vue 开发环境
  9. 韩语在线翻译图片识别_最强文字识别APP
  10. eclipse保存文件出现save could not be completed. Try File Save As..
  11. mysql全称量词_MySQL操作记录的方法集合,供以后查看
  12. 云存储服务OneDrive捆绑系统销售,30多家欧洲公司投诉微软垄断
  13. 不积跬步,无以至千里
  14. 计算机考研复试之KY122 找出直系亲属(c++)
  15. Ricochet —— 基于 Tor 的加密即时通信工具
  16. Windows10 LSTC 2021输入法无法使用的问题
  17. 2021年六级英文作文:China’s achievement in higher education.
  18. 前端树形图(未完成完善,会持续更新)
  19. 2020年中国研究生数学建模竞赛C题
  20. 计算机高级语言c高起专阶段性作业1,重庆大学网络教育高起专计算机应用基础入学测试模拟题及答案2...

热门文章

  1. LeetCode Palindrome Linked List
  2. 朴素贝叶斯-垃圾邮件分类实现
  3. python中is与==的差别
  4. xib自动布局的时候,label高度计算误差问题
  5. 搭建SSH框架之一(资料准备)
  6. 随笔--2011.12.21
  7. IBM HACMP 系列 -- 后期安装工作和管理任务二
  8. 三通道阈值化最好不要使用img_h<0>0.15img_v>0.25的写法~
  9. leetcode算法题--一周中的第几天
  10. linux进程--死锁产生的原因及四个必要条件(六)