DictionaryDropDownListExtensions.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. using Autofac;
  8. using Bowin.Web.Controls.Mvc;
  9. using EMIS.Utility;
  10. using EMIS.Entities;
  11. using EMIS.ViewModel;
  12. using EMIS.ViewModel.CacheManage;
  13. namespace EMIS.Web.Controls
  14. {
  15. public static class DictionaryDropDownListExtensions
  16. {
  17. /// <summary>
  18. /// 字典下拉菜单控件,下拉列出指定字典表中的枚举项
  19. /// </summary>
  20. /// <param name="htmlHelper"></param>
  21. /// <param name="dictionaryItem">指定要列出的字典类型</param>
  22. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  23. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  24. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  25. /// }
  26. /// </param>
  27. /// <returns></returns>
  28. public static MvcHtmlString DictionaryDropDownList(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  29. {
  30. return DictionaryDropDownListSearch(htmlHelper, dictionaryItem.ToString(), dropdownListOptions, htmlAttributes);
  31. }
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <param name="htmlHelper"></param>
  36. /// <param name="dictionaryCode"></param>
  37. /// <param name="dropdownListOptions"></param>
  38. /// <param name="htmlAttributes"></param>
  39. /// <returns></returns>
  40. private static MvcHtmlString DictionaryDropDownListSearch(HtmlHelper htmlHelper, string dictionaryCode, DropdownListOptions dropdownListOptions, IDictionary<string, string> htmlAttributes = null)
  41. {
  42. if (dropdownListOptions == null)
  43. dropdownListOptions = new DropdownListOptions();
  44. dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/DictionaryDropDown?dictionaryCode="
  45. + dictionaryCode, htmlHelper.ViewContext.HttpContext);
  46. if (dropdownListOptions.SelectedValue == null)
  47. {
  48. dropdownListOptions.SelectedValue = DropdownList.PLEASE_SELECT.ToString();
  49. }
  50. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  51. //return htmlHelper.ConvertToComboGrid(dictionaryCode, dropdownListOptions, htmlAttributes);
  52. }
  53. /// <summary>
  54. /// 字典下拉菜单控件,下拉列出指定字典表中的枚举项
  55. /// </summary>
  56. /// <param name="htmlHelper"></param>
  57. /// <param name="dictionaryItem">指定要列出的字典类型,用字符串表示</param>
  58. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  59. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  60. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  61. /// }
  62. /// </param>
  63. /// <returns></returns>
  64. public static MvcHtmlString DictionaryDropDownList(this HtmlHelper htmlHelper, string dictionaryItem, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  65. {
  66. return DictionaryDropDownListSearch(htmlHelper, dictionaryItem, dropdownListOptions, htmlAttributes);
  67. }
  68. /// <summary>
  69. /// 绑定ViewModel的字典下拉菜单控件,下拉列出指定字典表中的枚举项
  70. /// </summary>
  71. /// <typeparam name="TModel"></typeparam>
  72. /// <typeparam name="TProperty"></typeparam>
  73. /// <param name="htmlHelper"></param>
  74. /// <param name="dictionaryItem">指定要列出的字典类型</param>
  75. /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  76. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  77. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  78. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  79. /// }
  80. /// </param>
  81. /// <returns></returns>
  82. public static MvcHtmlString DictionaryDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, DictionaryItem dictionaryItem,
  83. Expression<Func<TModel, TProperty>> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  84. {
  85. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  86. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  87. //MVC验证支持
  88. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  89. if (htmlAttributes == null)
  90. {
  91. htmlAttributes = new Dictionary<string, string>();
  92. }
  93. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  94. return DictionaryDropDownListForBind(htmlHelper, dictionaryItem.ToString(), modelMetadata, dropdownListOptions, htmlAttributes, fullHtmlFieldName);
  95. }
  96. /// <summary>
  97. /// 绑定ViewModel的字典下拉菜单控件,下拉列出指定字典表中的枚举项
  98. /// </summary>
  99. /// <typeparam name="TModel"></typeparam>
  100. /// <typeparam name="TProperty"></typeparam>
  101. /// <param name="htmlHelper"></param>
  102. /// <param name="dictionaryItemName">指定要列出的字典类型,用字符串表示</param>
  103. /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  104. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  105. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  106. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  107. /// }
  108. /// </param>
  109. /// <returns></returns>
  110. public static MvcHtmlString DictionaryDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string dictionaryItemName,
  111. Expression<Func<TModel, TProperty>> expression, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  112. {
  113. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  114. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  115. //MVC验证支持
  116. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  117. if (htmlAttributes == null)
  118. {
  119. htmlAttributes = new Dictionary<string, string>();
  120. }
  121. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  122. return DictionaryDropDownListForBind(htmlHelper, dictionaryItemName, modelMetadata, dropdownListOptions, htmlAttributes, fullHtmlFieldName);
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <param name="htmlHelper"></param>
  128. /// <param name="dictionaryCode"></param>
  129. /// <param name="modelMetadata"></param>
  130. /// <param name="dropdownListOptions"></param>
  131. /// <param name="htmlAttributes"></param>
  132. /// <param name="fullHtmlFieldName"></param>
  133. /// <returns></returns>
  134. private static MvcHtmlString DictionaryDropDownListForBind(HtmlHelper htmlHelper, string dictionaryCode, ModelMetadata modelMetadata,
  135. DropdownListOptions dropdownListOptions, IDictionary<string, string> htmlAttributes, string fullHtmlFieldName)
  136. {
  137. if (dropdownListOptions == null)
  138. dropdownListOptions = new DropdownListOptions();
  139. dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/DictionaryDropDown?dictionaryCode="
  140. + dictionaryCode, htmlHelper.ViewContext.HttpContext);
  141. if (string.IsNullOrEmpty(dropdownListOptions.ID))
  142. {
  143. dropdownListOptions.ID = fullHtmlFieldName;
  144. }
  145. dropdownListOptions.Name = fullHtmlFieldName;
  146. dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT));
  147. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  148. //return htmlHelper.ConvertToComboGrid(dictionaryCode, dropdownListOptions, htmlAttributes);
  149. }
  150. /// <summary>
  151. /// 专为查询使用的字典下拉菜单控件,下拉列出指定字典表中的枚举项,不推荐使用
  152. /// </summary>
  153. /// <param name="htmlHelper"></param>
  154. /// <param name="dictionaryItem">指定要列出的字典类型</param>
  155. /// <param name="dataGridID">查询影响的数据列表控件ID</param>
  156. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  157. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  158. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  159. /// }
  160. /// </param>
  161. /// <returns></returns>
  162. public static MvcHtmlString DictionaryDropDownListForSearch(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, string dataGridID,
  163. DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  164. {
  165. if (dropdownListOptions == null)
  166. {
  167. dropdownListOptions = new DropdownListOptions() { Name = dictionaryItem.ToString() };
  168. }
  169. dropdownListOptions.BindType = DropdownListBindType.SelectAll;
  170. if (htmlAttributes == null)
  171. {
  172. htmlAttributes = new Dictionary<string, string>();
  173. }
  174. htmlAttributes.Add("data-condition", dataGridID);
  175. return htmlHelper.DictionaryDropDownList(dictionaryItem, dropdownListOptions, htmlAttributes);
  176. }
  177. /// <summary>
  178. ///
  179. /// </summary>
  180. /// <param name="htmlHelper"></param>
  181. /// <param name="TypeID"></param>
  182. /// <param name="dictionaryCode"></param>
  183. /// <returns></returns>
  184. public static MvcHtmlString DictionaryItemName(this HtmlHelper htmlHelper, int? TypeID, string dictionaryCode)
  185. {
  186. string Name = string.Empty;
  187. if (TypeID == null)
  188. Name = "";
  189. using (var scop = AutofacHelper.Container.BeginLifetimeScope())
  190. {
  191. var DictionaryServices = scop.Resolve<EMIS.CommonLogic.SystemSetting.IDictionaryServices>();
  192. HttpRequest request = HttpContext.Current.Request;
  193. Name = IdNameExt.GetDictionaryItem(dictionaryCode)
  194. .Where(x => x.Value == TypeID)
  195. .Select(x => x.Name).FirstOrDefault();
  196. }
  197. return MvcHtmlString.Create(Name);
  198. }
  199. /// <summary>
  200. /// 学年数下拉控件,下拉列出学年数,如:2015、2016、2017等等……
  201. /// </summary>
  202. /// <param name="htmlHelper"></param>
  203. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  204. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  205. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  206. /// }
  207. /// </param>
  208. /// <returns></returns>
  209. public static MvcHtmlString SchoolYearDropDownList(this HtmlHelper htmlHelper,DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  210. {
  211. if (dropdownListOptions == null)
  212. dropdownListOptions = new DropdownListOptions();
  213. dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/SchoolYearDropDown", htmlHelper.ViewContext.HttpContext);
  214. if (dropdownListOptions.SelectedValue == null)
  215. {
  216. dropdownListOptions.SelectedValue = DropdownList.PLEASE_SELECT.ToString();
  217. }
  218. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  219. }
  220. /// <summary>
  221. /// 绑定ViewModel的学年数下拉控件,下拉列出学年数,如:2015、2016、2017等等……
  222. /// </summary>
  223. /// <typeparam name="TModel"></typeparam>
  224. /// <typeparam name="TProperty"></typeparam>
  225. /// <param name="htmlHelper"></param>
  226. /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  227. /// <param name="dropdownListOptions">下拉菜单控件配置项,除ItemSourceUrl、TextField、ValueField、ItemSource外其他都生效</param>
  228. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  229. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  230. /// }
  231. /// </param>
  232. /// <returns></returns>
  233. public static MvcHtmlString SchoolYearDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression,
  234. DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  235. {
  236. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  237. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  238. //MVC验证支持
  239. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  240. if (htmlAttributes == null)
  241. {
  242. htmlAttributes = new Dictionary<string, string>();
  243. }
  244. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  245. if (dropdownListOptions == null)
  246. dropdownListOptions = new DropdownListOptions();
  247. dropdownListOptions.ItemSourceUrl = UrlHelper.GenerateContentUrl("~/Common/SchoolYearDropDown", htmlHelper.ViewContext.HttpContext);
  248. if (string.IsNullOrEmpty(dropdownListOptions.ID))
  249. {
  250. dropdownListOptions.ID = fullHtmlFieldName;
  251. }
  252. dropdownListOptions.Name = fullHtmlFieldName;
  253. dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT));
  254. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  255. }
  256. /// <summary>
  257. ///
  258. /// </summary>
  259. /// <param name="htmlHelper"></param>
  260. /// <param name="dictionaryCode"></param>
  261. /// <param name="dropdownListOptions"></param>
  262. /// <param name="htmlAttributes"></param>
  263. /// <returns></returns>
  264. private static MvcHtmlString ConvertToComboGrid(this HtmlHelper htmlHelper, string dictionaryCode, DropdownListOptions dropdownListOptions, IDictionary<string, string> htmlAttributes)
  265. {
  266. ComboGridOptions comboGridOption = new ComboGridOptions()
  267. {
  268. SelectedValue = dropdownListOptions.SelectedValue,
  269. TextField = dropdownListOptions.TextField ?? "Name",
  270. ValueField = dropdownListOptions.ValueField ?? "Value",
  271. OnSelect = dropdownListOptions.OnSelect,
  272. OnChange = dropdownListOptions.OnChange,
  273. Width = dropdownListOptions.Width,
  274. Height = dropdownListOptions.Height,
  275. IsEnabled = dropdownListOptions.IsEnabled,
  276. IsRequired = dropdownListOptions.IsRequired,
  277. IsAutoComplete = true,
  278. EmptyText = (dropdownListOptions.BindType == DropdownListBindType.SelectAll ? "全部" : (dropdownListOptions.BindType == DropdownListBindType.PleaseSelect ? "请选择" : ""))
  279. };
  280. comboGridOption.GridOptions = new DataGridOptions
  281. {
  282. Columns = new List<DataGridColumn>() {
  283. new BoundFieldColumn { FieldName="Value", Align=AlignStyle.Center },
  284. new BoundFieldColumn { FieldName="Name", Align=AlignStyle.Center, CustomFormatFun = dropdownListOptions.Formatter }
  285. },
  286. OnLoadSuccessFun = dropdownListOptions.OnLoadSuccess,
  287. IsShowTitle = false,
  288. IsCheckOnSelect = true,
  289. DataSourceUrl = UrlHelper.GenerateContentUrl("~/DictionaryItem/ComboGridList?dictionaryCode=" + dictionaryCode, htmlHelper.ViewContext.HttpContext),
  290. IsPagination = true,
  291. IsShowRowNumbers = true,
  292. IsSingleSelect = false
  293. };
  294. return MvcHtmlString.Create(ComboGrid.CreateControl(comboGridOption, htmlAttributes).Render());
  295. }
  296. /// <summary>
  297. ///
  298. /// </summary>
  299. /// <param name="htmlHelper"></param>
  300. /// <param name="dictionaryItem"></param>
  301. /// <param name="bindType"></param>
  302. /// <param name="comboGridOptions"></param>
  303. /// <returns></returns>
  304. private static ComboGridOptions InitComboGridOptions(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListBindType bindType, ComboGridOptions comboGridOptions)
  305. {
  306. if (comboGridOptions == null)
  307. comboGridOptions = new ComboGridOptions();
  308. if (comboGridOptions.GridOptions == null)
  309. comboGridOptions.GridOptions = new DataGridOptions();
  310. if (string.IsNullOrEmpty(comboGridOptions.TextField))
  311. {
  312. comboGridOptions.TextField = "Name";
  313. }
  314. if (string.IsNullOrEmpty(comboGridOptions.ValueField))
  315. {
  316. comboGridOptions.ValueField = "Value";
  317. }
  318. if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
  319. {
  320. comboGridOptions.EmptyText = (bindType == DropdownListBindType.SelectAll ? "全部" : (bindType == DropdownListBindType.PleaseSelect ? "请选择" : ""));
  321. }
  322. comboGridOptions.IsAutoComplete = comboGridOptions.IsAutoComplete ?? true;
  323. comboGridOptions.GridOptions.DataSourceUrl = UrlHelper.GenerateContentUrl("~/DictionaryItem/ComboGridList?dictionaryCode="
  324. + dictionaryItem.ToString(), htmlHelper.ViewContext.HttpContext);
  325. comboGridOptions.GridOptions.IsShowTitle = false;
  326. comboGridOptions.GridOptions.IsShowHeader = false;
  327. if (comboGridOptions.GridOptions.Columns == null || comboGridOptions.GridOptions.Columns.Count == 0)
  328. {
  329. comboGridOptions.GridOptions.Columns = new List<DataGridColumn>() {
  330. new BoundFieldColumn { FieldName="Name", Align=AlignStyle.Center }
  331. };
  332. }
  333. return comboGridOptions;
  334. }
  335. /// <summary>
  336. /// 字典下拉表格控件,下拉列出指定字典表中的枚举项
  337. /// </summary>
  338. /// <param name="htmlHelper"></param>
  339. /// <param name="dictionaryItem">指定要列出的字典类型</param>
  340. /// <param name="bindType">绑定类型,有三种方式,一般情况下,作为列表查询条件时,使用SelectAll,默认项显示"全部";
  341. /// 作为明细页面编辑控件时,使用PleaseSelect,默认项显示"请选择";
  342. /// 部分页面因为要求用户必须选择一个选项,例如报表的周期,这时候使用None,不显示默认项</param>
  343. /// <param name="comboGridOptions">下拉表格控件配置项,除GridOptions.ItemSourceUrl、EmptyText、TextField、ValueField外其他都生效</param>
  344. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  345. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  346. /// }
  347. /// </param>
  348. /// <returns></returns>
  349. public static MvcHtmlString DictionaryComboGrid(this HtmlHelper htmlHelper, DictionaryItem dictionaryItem, DropdownListBindType bindType,
  350. ComboGridOptions comboGridOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  351. {
  352. comboGridOptions = InitComboGridOptions(htmlHelper, dictionaryItem, bindType, comboGridOptions);
  353. return htmlHelper.ComboGrid(comboGridOptions, htmlAttributes);
  354. }
  355. /// <summary>
  356. /// 绑定ViewModel的字典下拉表格控件,下拉列出指定字典表中的枚举项
  357. /// </summary>
  358. /// <typeparam name="TModel"></typeparam>
  359. /// <typeparam name="TProperty"></typeparam>
  360. /// <param name="htmlHelper"></param>
  361. /// <param name="dictionaryItem">指定要列出的字典类型</param>
  362. /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  363. /// <param name="bindType">绑定类型,有三种方式,一般情况下,作为列表查询条件时,使用SelectAll,默认项显示"全部";
  364. /// 作为明细页面编辑控件时,使用PleaseSelect,默认项显示"请选择";
  365. /// 部分页面因为要求用户必须选择一个选项,例如报表的周期,这时候使用None,不显示默认项</param>
  366. /// <param name="comboGridOptions">下拉表格控件配置项,除GridOptions.ItemSourceUrl、EmptyText、TextField、ValueField外其他都生效</param>
  367. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  368. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  369. /// }
  370. /// </param>
  371. /// <returns></returns>
  372. public static MvcHtmlString DictionaryComboGridFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, DictionaryItem dictionaryItem, Expression<Func<TModel, TProperty>> expression, DropdownListBindType bindType,
  373. ComboGridOptions comboGridOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  374. {
  375. comboGridOptions = InitComboGridOptions(htmlHelper, dictionaryItem, bindType, comboGridOptions);
  376. return htmlHelper.ComboGridFor(expression, comboGridOptions, htmlAttributes);
  377. }
  378. /// <summary>
  379. ///
  380. /// </summary>
  381. /// <typeparam name="TModel"></typeparam>
  382. /// <typeparam name="TProperty"></typeparam>
  383. /// <param name="htmlHelper"></param>
  384. /// <param name="expression"></param>
  385. /// <param name="dropdownListOptions"></param>
  386. /// <param name="htmlAttributes"></param>
  387. /// <returns></returns>
  388. public static MvcHtmlString WeekdayDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression,
  389. DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  390. {
  391. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  392. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  393. //MVC验证支持
  394. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  395. if (htmlAttributes == null)
  396. {
  397. htmlAttributes = new Dictionary<string, string>();
  398. }
  399. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  400. if (dropdownListOptions == null)
  401. dropdownListOptions = new DropdownListOptions();
  402. dropdownListOptions.ItemList = BaseExtensions.WeekdayDropdownListItemList;
  403. if (string.IsNullOrEmpty(dropdownListOptions.ID))
  404. {
  405. dropdownListOptions.ID = fullHtmlFieldName;
  406. }
  407. dropdownListOptions.Name = fullHtmlFieldName;
  408. dropdownListOptions.SelectedValue = Convert.ToString((int)(modelMetadata.Model ?? DropdownList.PLEASE_SELECT));
  409. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  410. }
  411. /// <summary>
  412. ///
  413. /// </summary>
  414. /// <param name="htmlHelper"></param>
  415. /// <param name="dropdownListOptions"></param>
  416. /// <param name="htmlAttributes"></param>
  417. /// <returns></returns>
  418. public static MvcHtmlString WeekdayDropDownList(this HtmlHelper htmlHelper, DropdownListOptions dropdownListOptions = null, System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  419. {
  420. if (dropdownListOptions == null)
  421. {
  422. dropdownListOptions = new DropdownListOptions();
  423. }
  424. dropdownListOptions.ItemList = BaseExtensions.WeekdayDropdownListItemList;
  425. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  426. }
  427. }
  428. }