123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- using System.Linq.Expressions;
- namespace Bowin.Web.Controls.Mvc
- {
- public static class ComboGridExtensions
- {
- /// <summary>
- /// 下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
- /// </summary>
- /// <param name="htmlHelper"></param>
- /// <param name="comboGridOptions">下拉表格控件配置项</param>
- /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
- /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
- /// }</param>
- /// <returns></returns>
- public static System.Web.Mvc.MvcHtmlString ComboGrid(
- this System.Web.Mvc.HtmlHelper htmlHelper
- , ComboGridOptions comboGridOptions
- , IDictionary<string, string> htmlAttributes = null)
- {
- if (comboGridOptions == null)
- {
- comboGridOptions = new ComboGridOptions();
- }
- if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
- {
- comboGridOptions.EmptyText = "全部";
- }
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
- }
- /// <summary>
- /// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
- /// </summary>
- /// <typeparam name="TModel"></typeparam>
- /// <typeparam name="TProperty"></typeparam>
- /// <param name="htmlHelper"></param>
- /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
- /// <param name="comboGridOptions">下拉表格控件配置项</param>
- /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
- /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
- /// }</param>
- /// <returns></returns>
- public static MvcHtmlString ComboGridFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- ComboGridOptions comboGridOptions,
- System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
- {
- if (expression == null)
- {
- throw new ArgumentNullException("expression");
- }
- ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
- //获取值
- //modelMetadata.Model
- //获取字段名称,对应标签的name
- string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
- comboGridOptions.ID = fullHtmlFieldName;
- comboGridOptions.Name = fullHtmlFieldName;
- if (modelMetadata.Model != null)
- {
- comboGridOptions.SelectedValue = modelMetadata.Model;
- }
- //MVC验证支持
- var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
- if (htmlAttributes == null)
- {
- htmlAttributes = new Dictionary<string, string>();
- }
- validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
- if (!string.IsNullOrEmpty(comboGridOptions.GridOptions.DataSourceUrl))
- {
- var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
- comboGridOptions.GridOptions.DataSourceUrl = urlHelper.Content(comboGridOptions.GridOptions.DataSourceUrl);
- }
- if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
- {
- comboGridOptions.EmptyText = "请选择";
- }
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
- }
- /// <summary>
- /// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现(该重载主要用于少数特殊情况,建议尽量不要使用)
- /// </summary>
- /// <typeparam name="TModel"></typeparam>
- /// <typeparam name="TValueProperty"></typeparam>
- /// <param name="htmlHelper"></param>
- /// <param name="valueExpression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
- /// <param name="textExpression">定义显示文本属性的lamda,如:(x => x.Name)</param>
- /// <param name="comboGridOptions">下拉表格控件配置项</param>
- /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
- /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
- /// }</param>
- /// <returns></returns>
- public static MvcHtmlString ComboGridFor<TModel, TValueProperty>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TValueProperty>> valueExpression,
- Expression<Func<TModel, string>> textExpression,
- ComboGridOptions comboGridOptions,
- IDictionary<string, string> htmlAttributes = null)
- {
- if (valueExpression == null)
- {
- throw new ArgumentNullException("valueExpression");
- }
- if (textExpression == null)
- {
- throw new ArgumentNullException("textExpression");
- }
- string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(valueExpression));
- comboGridOptions.ID = fullHtmlFieldName;
- comboGridOptions.Name = fullHtmlFieldName;
- ModelMetadata modelValueMetadata = ModelMetadata.FromLambdaExpression<TModel, TValueProperty>(valueExpression, htmlHelper.ViewData);
- if (modelValueMetadata.Model != null)
- {
- comboGridOptions.SelectedValue = modelValueMetadata.Model;
- }
- ModelMetadata modelTextMetadata = ModelMetadata.FromLambdaExpression<TModel, string>(textExpression, htmlHelper.ViewData);
- if (modelTextMetadata.Model != null)
- {
- comboGridOptions.SelectedText = (string)modelTextMetadata.Model;
- }
- //MVC验证支持
- var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelValueMetadata);
- if (htmlAttributes == null)
- {
- htmlAttributes = new Dictionary<string, string>();
- }
- validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
- if (!string.IsNullOrEmpty(comboGridOptions.GridOptions.DataSourceUrl))
- {
- var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
- comboGridOptions.GridOptions.DataSourceUrl = urlHelper.Content(comboGridOptions.GridOptions.DataSourceUrl);
- }
- if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
- {
- comboGridOptions.EmptyText = "请选择";
- }
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
- }
- }
- }
|