mvc自带的DropDownListFor数据源必须是IEnumerable<SelectListItem>。并且option不支持增加自定义属性。在使用bootstrap-select组件的时候,发现不是很好用。所以扩展了一下。

当然,因为场景的问题,我不需要group,不需要selected,所以这部分没有完成,且相应的重载方法也没有写。只有一个core方法,算是一个半成品吧。

 public static MvcHtmlString DropDownListForEx<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, object>> expression, Func<TModel, IEnumerable<TProperty>> options,
Func<TProperty, string> optionText, Func<TProperty, string> optionValue, string optionLabel = null,
Func<TProperty, IDictionary<string, object>> optionHtmlAttributes = null, IDictionary<string, object> htmlAttributes = null)
{ var tagNameAttr = ExpressionHelper.GetExpressionText(expression);
TagBuilder tag = new TagBuilder("select");
if (htmlAttributes != null)
tag.MergeAttributes(htmlAttributes);
tag.MergeAttribute("name", tagNameAttr, true);
tag.GenerateId(tagNameAttr); StringBuilder optionsHtml = new StringBuilder();
var os = options(htmlHelper.ViewData.Model);
if (optionLabel != null)
{
TagBuilder tag3 = new TagBuilder("option");
tag3.SetInnerText(optionLabel);
optionsHtml.AppendLine(tag3.ToString(TagRenderMode.Normal));
} foreach (var item in os)
{
TagBuilder tag2 = new TagBuilder("option");
tag2.SetInnerText(optionText(item));
if (optionHtmlAttributes != null)
tag2.MergeAttributes(optionHtmlAttributes(item));
tag2.MergeAttribute("value", optionValue(item), true);
var oHtml = tag2.ToString(TagRenderMode.Normal);
optionsHtml.AppendLine(oHtml);
} tag.InnerHtml = optionsHtml.ToString();
return new MvcHtmlString(tag.ToString(TagRenderMode.Normal));
}

调用方式:

depInfos:List<>
 @Html.DropDownListForEx(p => p.DepCode, p => depInfos, p => p.Name, p => p.Id, "请选择", p => new Dictionary<string, object>() { { "data-tokens", p.NamePinYin+" "+p.NameFirstPinYin } }, new Dictionary<string, object> { { "class", "selectpicker" }, { "data-live-search", true } })

顺便介绍一下bootstrap-select组件。bootstrap框架下面的下拉选择组件,支持下拉搜索选择,关键字可自定义。上面的我的例子就是一个地址选择例子,使用地址的全拼,首字母拼音搜索

具体例子参考官方地址。有详细说明和demo

官方地址:http://silviomoreto.github.io/bootstrap-select

git地址:https://github.com/silviomoreto/bootstrap-select

官方例子截图:

扩展htmlhelper.DropDownListFor 支持list数据源和option增加属性的更多相关文章

  1. 试图使用未在此报表服务器中注册或此版 Reporting Services 不支持的数据扩展插件“Devart.Data.PostgreSql”

    数据源用的是Postgresql 我在Deploy Report的时候出现这条ErrorMessage Error 2 试图使用未在此报表服务器中注册或此版 Reporting Services 不支 ...

  2. 扩展GDAL,支持CNSDTF格式(一)

    扩展GDAL,支持CNSDTF格式(一) 一.        简介 本文主要根据<中华人民共和国国家标准GB/T17798-2007--地理空间数据交换格式(Geospatialdata tra ...

  3. Druid学习之路 (三)Druid的数据源和段

    作者:Syn良子 出处:https://www.cnblogs.com/cssdongl/p/9703204.html 转载请注明出处 Druid的数据源和分段 Druid的数据存储在"Da ...

  4. 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 ...

  5. Catalyst揭秘 Day8 Final 外部数据源和缓存系统

    Catalyst揭秘 Day8 Final 外部数据源和缓存系统 今天是Catalyst部分的收官,主要讲一些杂项内容. 外部数据源处理 什么叫外部数据源,是SparkSql自己支持的一些文件格式,以 ...

  6. 扩展SpringMVC以支持绑定JSON格式的请求参数

    此方案是把请求参数(JSON字符串)绑定到java对象,,@RequestBody是绑定内容体到java对象的. 问题描述: <span style="font-size: x-sma ...

  7. [asp.net mvc 奇淫巧技] 05 - 扩展ScriptBundle,支持混淆加密javascript

    一.需求: 在web开发中,经常会处理javascript的一些问题,其中就包括js的压缩,合并,发布版本以及混淆加密等等问题.在asp.net 开发中我们使用ScriptBundle已经可以解决ja ...

  8. 扩展HtmlHelper类实现Mvc4分页

    1.扩展HtmlHelper类方法Pager public static HtmlString Pager(this HtmlHelper htmlHelper, int currentPage, i ...

  9. OLEDB数据源和目标组件

    在SSIS工程的开发过程中,OLEDB 数据源和目标组件是最常用的数据流组件.从功能上讲,OLEDB 数据源组件用于从OLEDB 提供者(Provider)中获取数据,传递给下游组件,OLEDB提供者 ...

随机推荐

  1. SharePoint 2013 搭建app本地开发环境

    使用SharePoint App,如果要通过应用程序目录分发 SharePoint 相关应用程序,如具有完全控制权限的 SharePoint 相关应用程序(无法部署到 Office 365 网站),则 ...

  2. SharePoint 2013 文档库中PPT转换PDF

    通过使用 PowerPoint Automation Services,可以从 PowerPoint 二进制文件格式 (.ppt) 和 PowerPoint Open XML 文件格式 (.pptx) ...

  3. break、continue、return

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. SQL SERVER 中如何用脚本管理作业

    在SQL SERVER中用脚本管理作业,在绝大部分场景下,脚本都比UI界面管理作业要高效.简洁.打个简单的比方,如果你要查看作业的运行时长,如果用UI界面查看,100个作业,你就得在历史记录里面至少查 ...

  5. 搭建自己的PHP框架心得(二)

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  6. This text field does not specify an inputType or a hint

    android开发过程中突然发现的warning,EditText 报出 “This text field does not specify an inputType or a hint”   原因: ...

  7. Windows共享作为公司文件服务器的案例

    1.目录结构 → 主管 部门 → 员工 → Public 2.实现效果 每个部门一个目录 部门主管可以访问自己和部门员工的目录 部门员工只可访问自己的目录 公共目录Public部门所有人都可访问 3. ...

  8. WPF Tranform-Flip Image

    Use a ScaleTransform with a ScaleX of -1 for horizontal and ScaleY of -1 for vertical flipping, appl ...

  9. 旧文备份:CANopen协议PDO的几种传输方式

    (于2007.1.22) 由于PDO所传输的数据内容是无协议的且分配的标识符范围较SDO靠前,因此,其效率和优先级都是较高的,通常用于实时过程数据的传输. PDO是生产/消费类型的通讯方式,数据只有一 ...

  10. 2016.10.29 NOIP模拟赛 PM 考试整理

    300分的题,只得了第三题的100分. 题目+数据:链接:http://pan.baidu.com/s/1o7P4YXs 密码:4how T1:这道题目存在着诸多的问题: 1.开始的序列是无法消除的( ...