ComboGridExtensions.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public static class ComboGridExtensions
  10. {
  11. /// <summary>
  12. /// 下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
  13. /// </summary>
  14. /// <param name="htmlHelper"></param>
  15. /// <param name="comboGridOptions">下拉表格控件配置项</param>
  16. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  17. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  18. /// }</param>
  19. /// <returns></returns>
  20. public static System.Web.Mvc.MvcHtmlString ComboGrid(
  21. this System.Web.Mvc.HtmlHelper htmlHelper
  22. , ComboGridOptions comboGridOptions
  23. , IDictionary<string, string> htmlAttributes = null)
  24. {
  25. if (comboGridOptions == null)
  26. {
  27. comboGridOptions = new ComboGridOptions();
  28. }
  29. if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
  30. {
  31. comboGridOptions.EmptyText = "全部";
  32. }
  33. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
  34. }
  35. /// <summary>
  36. /// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现
  37. /// </summary>
  38. /// <typeparam name="TModel"></typeparam>
  39. /// <typeparam name="TProperty"></typeparam>
  40. /// <param name="htmlHelper"></param>
  41. /// <param name="expression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  42. /// <param name="comboGridOptions">下拉表格控件配置项</param>
  43. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  44. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  45. /// }</param>
  46. /// <returns></returns>
  47. public static MvcHtmlString ComboGridFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
  48. Expression<Func<TModel, TProperty>> expression,
  49. ComboGridOptions comboGridOptions,
  50. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  51. {
  52. if (expression == null)
  53. {
  54. throw new ArgumentNullException("expression");
  55. }
  56. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  57. //获取值
  58. //modelMetadata.Model
  59. //获取字段名称,对应标签的name
  60. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  61. comboGridOptions.ID = fullHtmlFieldName;
  62. comboGridOptions.Name = fullHtmlFieldName;
  63. if (modelMetadata.Model != null)
  64. {
  65. comboGridOptions.SelectedValue = modelMetadata.Model;
  66. }
  67. //MVC验证支持
  68. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  69. if (htmlAttributes == null)
  70. {
  71. htmlAttributes = new Dictionary<string, string>();
  72. }
  73. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  74. if (!string.IsNullOrEmpty(comboGridOptions.GridOptions.DataSourceUrl))
  75. {
  76. var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  77. comboGridOptions.GridOptions.DataSourceUrl = urlHelper.Content(comboGridOptions.GridOptions.DataSourceUrl);
  78. }
  79. if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
  80. {
  81. comboGridOptions.EmptyText = "请选择";
  82. }
  83. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
  84. }
  85. /// <summary>
  86. /// 绑定ViewModel的下拉表格控件(下拉后显示数据表格),可带搜索,数据表格部分主要通过DataGrid控件实现(该重载主要用于少数特殊情况,建议尽量不要使用)
  87. /// </summary>
  88. /// <typeparam name="TModel"></typeparam>
  89. /// <typeparam name="TValueProperty"></typeparam>
  90. /// <param name="htmlHelper"></param>
  91. /// <param name="valueExpression">定义绑定属性的lamda,如:(x => x.LoginID)</param>
  92. /// <param name="textExpression">定义显示文本属性的lamda,如:(x => x.Name)</param>
  93. /// <param name="comboGridOptions">下拉表格控件配置项</param>
  94. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  95. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  96. /// }</param>
  97. /// <returns></returns>
  98. public static MvcHtmlString ComboGridFor<TModel, TValueProperty>(this HtmlHelper<TModel> htmlHelper,
  99. Expression<Func<TModel, TValueProperty>> valueExpression,
  100. Expression<Func<TModel, string>> textExpression,
  101. ComboGridOptions comboGridOptions,
  102. IDictionary<string, string> htmlAttributes = null)
  103. {
  104. if (valueExpression == null)
  105. {
  106. throw new ArgumentNullException("valueExpression");
  107. }
  108. if (textExpression == null)
  109. {
  110. throw new ArgumentNullException("textExpression");
  111. }
  112. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(valueExpression));
  113. comboGridOptions.ID = fullHtmlFieldName;
  114. comboGridOptions.Name = fullHtmlFieldName;
  115. ModelMetadata modelValueMetadata = ModelMetadata.FromLambdaExpression<TModel, TValueProperty>(valueExpression, htmlHelper.ViewData);
  116. if (modelValueMetadata.Model != null)
  117. {
  118. comboGridOptions.SelectedValue = modelValueMetadata.Model;
  119. }
  120. ModelMetadata modelTextMetadata = ModelMetadata.FromLambdaExpression<TModel, string>(textExpression, htmlHelper.ViewData);
  121. if (modelTextMetadata.Model != null)
  122. {
  123. comboGridOptions.SelectedText = (string)modelTextMetadata.Model;
  124. }
  125. //MVC验证支持
  126. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelValueMetadata);
  127. if (htmlAttributes == null)
  128. {
  129. htmlAttributes = new Dictionary<string, string>();
  130. }
  131. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  132. if (!string.IsNullOrEmpty(comboGridOptions.GridOptions.DataSourceUrl))
  133. {
  134. var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  135. comboGridOptions.GridOptions.DataSourceUrl = urlHelper.Content(comboGridOptions.GridOptions.DataSourceUrl);
  136. }
  137. if (string.IsNullOrEmpty(comboGridOptions.EmptyText))
  138. {
  139. comboGridOptions.EmptyText = "请选择";
  140. }
  141. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.ComboGrid.CreateControl(comboGridOptions, htmlAttributes).Render());
  142. }
  143. }
  144. }