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
{
///
/// 下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
///
///
/// 下拉表格控件配置项
/// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary {
/// { "style", "width: 100%;" }, { "name", "ddlUsers" }
/// }
///
public static System.Web.Mvc.MvcHtmlString ComboGrid(
this System.Web.Mvc.HtmlHelper htmlHelper
, ComboGridOptions comboGridOptions
, IDictionary 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());
}
///
/// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
///
///
///
///
/// 定义绑定属性的lamda,如:(x => x.LoginID)
/// 下拉表格控件配置项
/// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary {
/// { "style", "width: 100%;" }, { "name", "ddlUsers" }
/// }
///
public static MvcHtmlString ComboGridFor(this HtmlHelper htmlHelper,
Expression> expression,
ComboGridOptions comboGridOptions,
System.Collections.Generic.IDictionary htmlAttributes = null)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(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();
}
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());
}
///
/// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现(该重载主要用于少数特殊情况,建议尽量不要使用)
///
///
///
///
/// 定义绑定属性的lamda,如:(x => x.LoginID)
/// 定义显示文本属性的lamda,如:(x => x.Name)
/// 下拉表格控件配置项
/// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary {
/// { "style", "width: 100%;" }, { "name", "ddlUsers" }
/// }
///
public static MvcHtmlString ComboGridFor(this HtmlHelper htmlHelper,
Expression> valueExpression,
Expression> textExpression,
ComboGridOptions comboGridOptions,
IDictionary 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(valueExpression, htmlHelper.ViewData);
if (modelValueMetadata.Model != null)
{
comboGridOptions.SelectedValue = modelValueMetadata.Model;
}
ModelMetadata modelTextMetadata = ModelMetadata.FromLambdaExpression(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();
}
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());
}
}
}