using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Linq.Expressions; using Autofac; using Bowin.Web.Controls.Mvc; using EMIS.Utility; using EMIS.Entities; using EMIS.ViewModel; using EMIS.ViewModel.CacheManage; namespace EMIS.Web.Controls { public static class DictionaryDropDownListExtensions { /// /// 字典下拉菜单控件,下拉列出指定字典表中的枚举项 /// /// /// 指定要列出的字典类型 /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryDropDownList(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { return DictionaryDropDownListSearch(htmlHelper, dictionaryItem.ToString(), dropdownListOptions, htmlAttributes); } /// /// /// /// /// /// /// /// private static MvcHtmlString DictionaryDropDownListSearch(HtmlHelper htmlHelper, string dictionaryCode, DropdownListOptions dropdownListOptions, IDictionary htmlAttributes = null) { if (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/DictionaryDropDown?dictionaryCode=" + dictionaryCode, htmlHelper.ViewContext.HttpContext); if (dropdownListOptions.SelectedValue == null) { dropdownListOptions.SelectedValue = DropdownList.PLEASE_SELECT.ToString(); } return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); //return htmlHelper.ConvertToComboGrid(dictionaryCode, dropdownListOptions, htmlAttributes); } /// /// 字典下拉菜单控件,下拉列出指定字典表中的枚举项 /// /// /// 指定要列出的字典类型,用字符串表示 /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryDropDownList(this HtmlHelper htmlHelper, string dictionaryItem, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { return DictionaryDropDownListSearch(htmlHelper, dictionaryItem, dropdownListOptions, htmlAttributes); } /// /// 绑定ViewModel的字典下拉菜单控件,下拉列出指定字典表中的枚举项 /// /// /// /// /// 指定要列出的字典类型 /// 定义绑定属性的lamda,如:(x => x.LoginID) /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryDropDownListFor(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, Expression> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); //MVC验证支持 var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString())); return DictionaryDropDownListForBind(htmlHelper, dictionaryItem.ToString(), modelMetadata, dropdownListOptions, htmlAttributes, fullHtmlFieldName); } /// /// 绑定ViewModel的字典下拉菜单控件,下拉列出指定字典表中的枚举项 /// /// /// /// /// 指定要列出的字典类型,用字符串表示 /// 定义绑定属性的lamda,如:(x => x.LoginID) /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryDropDownListFor(this HtmlHelper htmlHelper, string dictionaryItemName, Expression> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); //MVC验证支持 var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString())); return DictionaryDropDownListForBind(htmlHelper, dictionaryItemName, modelMetadata, dropdownListOptions, htmlAttributes, fullHtmlFieldName); } /// /// /// /// /// /// /// /// /// /// private static MvcHtmlString DictionaryDropDownListForBind(HtmlHelper htmlHelper, string dictionaryCode, ModelMetadata modelMetadata, DropdownListOptions dropdownListOptions, IDictionary htmlAttributes, string fullHtmlFieldName) { if (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/DictionaryDropDown?dictionaryCode=" + dictionaryCode, htmlHelper.ViewContext.HttpContext); if (string.IsNullOrEmpty(dropdownListOptions.ID)) { dropdownListOptions.ID = fullHtmlFieldName; } dropdownListOptions.Name = fullHtmlFieldName; dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT)); return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); //return htmlHelper.ConvertToComboGrid(dictionaryCode, dropdownListOptions, htmlAttributes); } /// /// 专为查询使用的字典下拉菜单控件,下拉列出指定字典表中的枚举项,不推荐使用 /// /// /// 指定要列出的字典类型 /// 查询影响的数据列表控件ID /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryDropDownListForSearch(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, string dataGridID, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { if (dropdownListOptions == null) { dropdownListOptions = new DropdownListOptions() { Name = dictionaryItem.ToString() }; } dropdownListOptions.BindType = DropdownListBindType.SelectAll; if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } htmlAttributes.Add("data-condition", dataGridID); return htmlHelper.DictionaryDropDownList(dictionaryItem, dropdownListOptions, htmlAttributes); } /// /// /// /// /// /// /// public static MvcHtmlString DictionaryItemName(this HtmlHelper htmlHelper, int? TypeID, string dictionaryCode) { string Name = string.Empty; if (TypeID == null) Name = ""; using (var scop = AutofacHelper.Container.BeginLifetimeScope()) { var DictionaryServices = scop.Resolve(); HttpRequest request = HttpContext.Current.Request; Name = IdNameExt.GetDictionaryItem(dictionaryCode) .Where(x => x.Value == TypeID) .Select(x => x.Name).FirstOrDefault(); } return MvcHtmlString.Create(Name); } /// /// 学年数下拉控件,下拉列出学年数,如:2015、2016、2017等等…… /// /// /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString SchoolYearDropDownList(this HtmlHelper htmlHelper,DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { if (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/SchoolYearDropDown", htmlHelper.ViewContext.HttpContext); if (dropdownListOptions.SelectedValue == null) { dropdownListOptions.SelectedValue = DropdownList.PLEASE_SELECT.ToString(); } return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); } /// /// 绑定ViewModel的学年数下拉控件,下拉列出学年数,如:2015、2016、2017等等…… /// /// /// /// /// 定义绑定属性的lamda,如:(x => x.LoginID) /// 下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString SchoolYearDropDownListFor(this HtmlHelper htmlHelper, Expression> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); //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 (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/SchoolYearDropDown", htmlHelper.ViewContext.HttpContext); if (string.IsNullOrEmpty(dropdownListOptions.ID)) { dropdownListOptions.ID = fullHtmlFieldName; } dropdownListOptions.Name = fullHtmlFieldName; dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT)); return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); } /// /// /// /// /// /// /// /// private static MvcHtmlString ConvertToComboGrid(this HtmlHelper htmlHelper, string dictionaryCode, DropdownListOptions dropdownListOptions, IDictionary htmlAttributes) { ComboGridOptions comboGridOption = new ComboGridOptions() { SelectedValue = dropdownListOptions.SelectedValue, TextField = dropdownListOptions.TextField ?? "Name", ValueField = dropdownListOptions.ValueField ?? "Value", OnSelect = dropdownListOptions.OnSelect, OnChange = dropdownListOptions.OnChange, Width = dropdownListOptions.Width, Height = dropdownListOptions.Height, IsEnabled = dropdownListOptions.IsEnabled, IsRequired = dropdownListOptions.IsRequired, IsAutoComplete = true, EmptyText = (dropdownListOptions.BindType == DropdownListBindType.SelectAll ? "全部" : (dropdownListOptions.BindType == DropdownListBindType.PleaseSelect ? "请选择" : "")) }; comboGridOption.GridOptions = new DataGridOptions { Columns = new List() { new BoundFieldColumn { FieldName="Value", Align=AlignStyle.Center }, new BoundFieldColumn { FieldName="Name", Align=AlignStyle.Center, CustomFormatFun = dropdownListOptions.Formatter } }, OnLoadSuccessFun = dropdownListOptions.OnLoadSuccess, IsShowTitle = false, IsCheckOnSelect = true, DataSourceUrl = UrlHelper.GenerateContentUrl("~/DictionaryItem/ComboGridList?dictionaryCode=" + dictionaryCode, htmlHelper.ViewContext.HttpContext), IsPagination = true, IsShowRowNumbers = true, IsSingleSelect = false }; return MvcHtmlString.Create(ComboGrid.CreateControl(comboGridOption, htmlAttributes).Render()); } /// /// /// /// /// /// /// /// private static ComboGridOptions InitComboGridOptions(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListBindType bindType, ComboGridOptions comboGridOptions) { if (comboGridOptions == null) comboGridOptions = new ComboGridOptions(); if (comboGridOptions.GridOptions == null) comboGridOptions.GridOptions = new DataGridOptions(); if (string.IsNullOrEmpty(comboGridOptions.TextField)) { comboGridOptions.TextField = "Name"; } if (string.IsNullOrEmpty(comboGridOptions.ValueField)) { comboGridOptions.ValueField = "Value"; } if (string.IsNullOrEmpty(comboGridOptions.EmptyText)) { comboGridOptions.EmptyText = (bindType == DropdownListBindType.SelectAll ? "全部" : (bindType == DropdownListBindType.PleaseSelect ? "请选择" : "")); } comboGridOptions.IsAutoComplete = comboGridOptions.IsAutoComplete ?? true; comboGridOptions.GridOptions.DataSourceUrl = UrlHelper.GenerateContentUrl("~/DictionaryItem/ComboGridList?dictionaryCode=" + dictionaryItem.ToString(), htmlHelper.ViewContext.HttpContext); comboGridOptions.GridOptions.IsShowTitle = false; comboGridOptions.GridOptions.IsShowHeader = false; if (comboGridOptions.GridOptions.Columns == null || comboGridOptions.GridOptions.Columns.Count == 0) { comboGridOptions.GridOptions.Columns = new List() { new BoundFieldColumn { FieldName="Name", Align=AlignStyle.Center } }; } return comboGridOptions; } /// /// 字典下拉表格控件,下拉列出指定字典表中的枚举项 /// /// /// 指定要列出的字典类型 /// 绑定类型,有三种方式,一般情况下,作为列表查询条件时,使用SelectAll,默认项显示"全部"; /// 作为明细页面编辑控件时,使用PleaseSelect,默认项显示"请选择"; /// 部分页面因为要求用户必须选择一个选项,例如报表的周期,这时候使用None,不显示默认项 /// 下拉表格控件配置项,除GridOptions.ItemSourceUrl、EmptyText、TextField、ValueField外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryComboGrid(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListBindType bindType, ComboGridOptions comboGridOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { comboGridOptions = InitComboGridOptions(htmlHelper, dictionaryItem, bindType, comboGridOptions); return htmlHelper.ComboGrid(comboGridOptions, htmlAttributes); } /// /// 绑定ViewModel的字典下拉表格控件,下拉列出指定字典表中的枚举项 /// /// /// /// /// 指定要列出的字典类型 /// 定义绑定属性的lamda,如:(x => x.LoginID) /// 绑定类型,有三种方式,一般情况下,作为列表查询条件时,使用SelectAll,默认项显示"全部"; /// 作为明细页面编辑控件时,使用PleaseSelect,默认项显示"请选择"; /// 部分页面因为要求用户必须选择一个选项,例如报表的周期,这时候使用None,不显示默认项 /// 下拉表格控件配置项,除GridOptions.ItemSourceUrl、EmptyText、TextField、ValueField外其他都生效 /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString DictionaryComboGridFor(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, Expression> expression, DropdownListBindType bindType, ComboGridOptions comboGridOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { comboGridOptions = InitComboGridOptions(htmlHelper, dictionaryItem, bindType, comboGridOptions); return htmlHelper.ComboGridFor(expression, comboGridOptions, htmlAttributes); } /// /// /// /// /// /// /// /// /// /// public static MvcHtmlString WeekdayDropDownListFor(this HtmlHelper htmlHelper, Expression> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); //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 (dropdownListOptions == null) dropdownListOptions = new DropdownListOptions(); dropdownListOptions.ItemList = BaseExtensions.WeekdayDropdownListItemList; if (string.IsNullOrEmpty(dropdownListOptions.ID)) { dropdownListOptions.ID = fullHtmlFieldName; } dropdownListOptions.Name = fullHtmlFieldName; dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT)); return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); } /// /// /// /// /// /// /// public static MvcHtmlString WeekdayDropDownList(this HtmlHelper htmlHelper, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { if (dropdownListOptions == null) { dropdownListOptions = new DropdownListOptions(); } dropdownListOptions.ItemList = BaseExtensions.WeekdayDropdownListItemList; return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes); } } }