扩展HtmlHelper
eg3:扩展HtmlHelper
扩展方法类

1 public static class HtmlExtension
2 {
3 /// <summary>
4 /// 输出表单提交按钮
5 /// </summary>
6 /// <param name="htmlHelper">HtmlHelper对象</param>
7 /// <param name="name"></param>
8 /// <param name="value"></param>
9 /// <returns></returns>
10 public static MvcHtmlString Submit(this HtmlHelper htmlHelper,string name,string value)
11 {
12 //TagBuilder 用于辅助创建标记
13 var builder = new TagBuilder("input");
14 builder.MergeAttribute("type","submit");
15 //设置value属性
16 builder.MergeAttribute("value",value);
17 //设置name属性
18 builder.MergeAttribute("name",name);
19 //设置id属性
20 builder.GenerateId(name);
21 return MvcHtmlString.Create(builder.ToString());
22 }
23
24 public static MvcHtmlString Submit(this HtmlHelper htmlHelper, string name, string value, object htmlAttributes)
25 {
26 //TagBuilder 用于辅助创建标记
27 var builder = new TagBuilder("input");
28 builder.MergeAttribute("type", "submit");
29 //设置value属性
30 builder.MergeAttribute("value", value);
31 //设置name属性
32 builder.MergeAttribute("name", name);
33 //设置id属性
34 builder.GenerateId(name);
35 //设置其他属性
36 builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
37 return MvcHtmlString.Create(builder.ToString());
38 }
39 }

//页面中进行使用

1 @{
2 Layout = null;
3 }
4 @using ExtensionMethods.App_Code;
5
6 <!DOCTYPE html>
7
8 <html>
9 <head>
10 <meta name="viewport" content="width=device-width" />
11 <title>Index</title>
12 </head>
13 <body>
14 <div>
15 @Html.Submit("login","登录")
16
17 @Html.Submit("login", "登录", new { @class="opt_sub",title="点击提交"})
18 </div>
19 </body>
20 </html>
扩展HtmlHelper的更多相关文章
- ASP.NET MVC 扩展HtmlHelper类方法
1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- 扩展HtmlHelper方法
1.在Model中新建类MyHtmlHelperExt /// <summary> /// 扩展HtmlHelper方法 /// 扩展方法三要素:静态类,静态方法,this关键字 /// ...
- 扩展HtmlHelper类实现Mvc4分页
1.扩展HtmlHelper类方法Pager public static HtmlString Pager(this HtmlHelper htmlHelper, int currentPage, i ...
- 扩展htmlhelper.DropDownListFor 支持list数据源和option增加属性
mvc自带的DropDownListFor数据源必须是IEnumerable<SelectListItem>.并且option不支持增加自定义属性.在使用bootstrap-select组 ...
- MVC教程七:扩展HtmlHelper方法
在上一篇文章的最后,列出了一些常见的HtmlHelper的方法,这些都是ASP.NET MVC已经定义好的,如果我们想自己定义一个HtmlHelper方法可以吗?答案是肯定的,那么如何自定义一个Htm ...
- MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList
代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...
- ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号
写在前面 在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服 ...
- 【asp.net mvc】 扩展 htmlhelper 实现分页
参考文档:http://www.cnblogs.com/caofangsheng/p/5670071.html http://www.cnblogs.com/arte ...
- Asp.Net Mvc4分页,扩展HtmlHelper类
1.分页方法 using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; ...
随机推荐
- SV学习之interface
普通的模块使用法:注意我们这里只实现了部分功能....不是完全的读写模块.... module mem_core( input logic wen, input logic ren, ...
- 分分钟钟学会Python - 模块
目录 模块 1 模块基础知识 2 random 返回随机生成的一个实数 3 hashlib 摘要算法模块 Hmac算法 4 time /datetime 时间模块 5 getpass 密码不显示(只能 ...
- fit in gnuplot
Table of Contents 1. fit-gnuplot 1 fit-gnuplot syntax >> fit [xrange][yrange] function 'datafi ...
- No unique bean of type..... Unsatisfied dependency of type
比如在XXXServiceImpl里面写了aa()方法给别的地方调用 但是自己又调用了自己 在开头写了 @Autowired Private XXX xxx; xxx.aa(); 这样重复调用自己的b ...
- Apache手册
一.apache的安装 如果不指定安装位置,默认为/usr/local/apache2/
- 【01】Firebug 教程
Firebug 教程 什么是 Firebug? Firebug 是一个开源的web开发工具. 现在浏览器自带firebug了. 安装 Firebug Firebug下载地址: https: ...
- HDU 2462 The Luckiest number
The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...
- Sql Server数据库视图的创建、修改
if OBJECT_ID('Sales.USACusts') is not null drop view Sales.USACusts; go create view Sales.USACusts a ...
- dfs树上的边
by GeneralLiu 一 开 始 学 tarjan 的 强连通分量 , 割边 , 割点 时 没有 学扎实 经过培训 ,发现了些 需要注意的 小细节 举个荔枝 dfs树 上的 边 学了 tar ...
- [BZOJ2120][BZOJ2453]数颜色
[BZOJ2120]数颜色 试题描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中 ...