MinOrEqualDropDownListExtensions.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 Bowin.Web.Controls.Mvc;
  8. namespace EMIS.Web.Controls
  9. {
  10. public static class MinOrEqualDropDownListExtensions
  11. {
  12. public static MvcHtmlString MinOrEqualDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
  13. Expression<Func<TModel, TProperty>> expression,
  14. DropdownListOptions dropdownListOptions = null,
  15. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  16. {
  17. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  18. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  19. //MVC验证支持
  20. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  21. if (htmlAttributes == null)
  22. {
  23. htmlAttributes = new Dictionary<string, string>();
  24. }
  25. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  26. if (dropdownListOptions == null)
  27. dropdownListOptions = new DropdownListOptions();
  28. dropdownListOptions.ItemList = new List<DropdownListItem>
  29. {
  30. new DropdownListItem { Text = "<", Value = "<" },
  31. new DropdownListItem { Text = "<=", Value = "<=" }
  32. };
  33. if (string.IsNullOrEmpty(dropdownListOptions.ID))
  34. {
  35. dropdownListOptions.ID = fullHtmlFieldName;
  36. }
  37. dropdownListOptions.Name = fullHtmlFieldName;
  38. dropdownListOptions.SelectedValue = (modelMetadata.Model ?? "<");
  39. return htmlHelper.DropdownList(dropdownListOptions, htmlAttributes);
  40. }
  41. }
  42. }