DictionaryDropDownListExtensions.cs 24 KB

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