PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/SystemWebMvc/Mvc/Html/SelectExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 174 lines | 144 code | 26 blank | 4 comment | 16 complexity | f3e6e8f03bff68b1e75fef4986ecb469 MD5 | raw file
  1. namespace System.Web.Mvc.Html {
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Globalization;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.Mvc.Resources;
  9. using System.Web.Routing;
  10. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  11. public static class SelectExtensions {
  12. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name) {
  13. return DropDownList(htmlHelper, optionLabel, name, (IDictionary<string, object>)null);
  14. }
  15. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name, object htmlAttributes) {
  16. return DropDownList(htmlHelper, optionLabel, name, new RouteValueDictionary(htmlAttributes));
  17. }
  18. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name, IDictionary<string, object> htmlAttributes) {
  19. SelectList selectList = htmlHelper.GetSelectData<SelectList>(name);
  20. return htmlHelper.SelectInternal(optionLabel, name, selectList, true /* usedViewData */, false /* allowMultiple */, htmlAttributes);
  21. }
  22. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name, SelectList selectList) {
  23. return DropDownList(htmlHelper, optionLabel, name, selectList, (IDictionary<string, object>)null);
  24. }
  25. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name, SelectList selectList, object htmlAttributes) {
  26. return DropDownList(htmlHelper, optionLabel, name, selectList, new RouteValueDictionary(htmlAttributes));
  27. }
  28. public static string DropDownList(this HtmlHelper htmlHelper, string name) {
  29. return DropDownList(htmlHelper, name, (object)null /* htmlAttributes */);
  30. }
  31. public static string DropDownList(this HtmlHelper htmlHelper, string name, object htmlAttributes) {
  32. return DropDownList(htmlHelper, name, new RouteValueDictionary(htmlAttributes));
  33. }
  34. public static string DropDownList(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {
  35. SelectList selectList = htmlHelper.GetSelectData<SelectList>(name);
  36. return htmlHelper.SelectInternal(null /* optionLabel */, name, selectList, true /* usedViewData */, false /* allowMultiple */, htmlAttributes);
  37. }
  38. public static string DropDownList(this HtmlHelper htmlHelper, string name, SelectList selectList) {
  39. return DropDownList(htmlHelper, name, selectList, (object)null /* htmlAttributes */);
  40. }
  41. public static string DropDownList(this HtmlHelper htmlHelper, string name, SelectList selectList, object htmlAttributes) {
  42. return DropDownList(htmlHelper, name, selectList, new RouteValueDictionary(htmlAttributes));
  43. }
  44. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
  45. Justification = "This type is appropriate for indicating a single selection.")]
  46. public static string DropDownList(this HtmlHelper htmlHelper, string name, SelectList selectList, IDictionary<string, object> htmlAttributes) {
  47. return htmlHelper.SelectInternal(null /* optionLabel */, name, selectList, false /* usedViewData */, false /* allowMultiple */, htmlAttributes);
  48. }
  49. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
  50. Justification = "This type is appropriate for indicating a single selection.")]
  51. public static string DropDownList(this HtmlHelper htmlHelper, string optionLabel, string name, SelectList selectList, IDictionary<string, object> htmlAttributes) {
  52. return htmlHelper.SelectInternal(optionLabel, name, selectList, false /* usedViewData */, false /* allowMultiple */, htmlAttributes);
  53. }
  54. public static string ListBox(this HtmlHelper htmlHelper, string name) {
  55. return ListBox(htmlHelper, name, (IDictionary<string, object>)null);
  56. }
  57. public static string ListBox(this HtmlHelper htmlHelper, string name, object htmlAttributes) {
  58. return ListBox(htmlHelper, name, new RouteValueDictionary(htmlAttributes));
  59. }
  60. public static string ListBox(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {
  61. MultiSelectList selectList = htmlHelper.GetSelectData<MultiSelectList>(name);
  62. return htmlHelper.SelectInternal(null /* optionLabel */, name, selectList, true /* usedViewData */, true /* allowMultiple */, htmlAttributes);
  63. }
  64. public static string ListBox(this HtmlHelper htmlHelper, string name, MultiSelectList selectList) {
  65. return ListBox(htmlHelper, name, selectList, (IDictionary<string, object>)null);
  66. }
  67. public static string ListBox(this HtmlHelper htmlHelper, string name, MultiSelectList selectList, object htmlAttributes) {
  68. return ListBox(htmlHelper, name, selectList, new RouteValueDictionary(htmlAttributes));
  69. }
  70. public static string ListBox(this HtmlHelper htmlHelper, string name, MultiSelectList selectList, IDictionary<string, object> htmlAttributes) {
  71. return htmlHelper.SelectInternal(null /* optionLabel */, name, selectList, false /* usedViewData */, true /* allowMultiple */, htmlAttributes);
  72. }
  73. private static TList GetSelectData<TList>(this HtmlHelper htmlHelper, string name) where TList : MultiSelectList {
  74. object o = null;
  75. if (htmlHelper.ViewData != null) {
  76. o = htmlHelper.ViewData.Eval(name);
  77. }
  78. if (o == null) {
  79. throw new InvalidOperationException(
  80. String.Format(
  81. CultureInfo.CurrentUICulture,
  82. MvcResources.HtmlHelper_MissingSelectData,
  83. name,
  84. typeof(TList)));
  85. }
  86. TList selectList = o as TList;
  87. if (selectList == null) {
  88. throw new InvalidOperationException(
  89. String.Format(
  90. CultureInfo.CurrentUICulture,
  91. MvcResources.HtmlHelper_WrongSelectDataType,
  92. name,
  93. o.GetType().FullName,
  94. typeof(TList)));
  95. }
  96. return selectList;
  97. }
  98. private static string ListItemToOption(ListItem item) {
  99. TagBuilder builder = new TagBuilder("option") {
  100. InnerHtml = HttpUtility.HtmlEncode(item.Text)
  101. };
  102. if (item.Value != null) {
  103. builder.Attributes["value"] = item.Value;
  104. }
  105. if (item.Selected) {
  106. builder.Attributes["selected"] = "selected";
  107. }
  108. return builder.ToString(TagRenderMode.Normal);
  109. }
  110. private static string SelectInternal(this HtmlHelper htmlHelper, string optionLabel, string name, MultiSelectList selectList, bool usedViewData, bool allowMultiple, IDictionary<string, object> htmlAttributes) {
  111. if (String.IsNullOrEmpty(name)) {
  112. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
  113. }
  114. if (selectList == null) {
  115. throw new ArgumentNullException("selectList");
  116. }
  117. // If we haven't already used ViewData to get the entire list of items then we need to
  118. // use the ViewData-supplied value before using the parameter-supplied value.
  119. if (!usedViewData) {
  120. object defaultValue;
  121. if (htmlHelper.ViewData.TryGetValue(name, out defaultValue)) {
  122. selectList = new MultiSelectList(selectList.Items, selectList.DataValueField, selectList.DataTextField,
  123. (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue });
  124. }
  125. }
  126. // Convert each ListItem to an <option> tag
  127. StringBuilder listItemBuilder = new StringBuilder();
  128. IList<ListItem> listItems = selectList.GetListItems();
  129. // Make optionLabel the first item that gets rendered.
  130. if (!String.IsNullOrEmpty(optionLabel)) {
  131. listItemBuilder.AppendLine(ListItemToOption(new ListItem() { Text = optionLabel, Value = String.Empty, Selected = false }));
  132. }
  133. foreach (ListItem item in listItems) {
  134. listItemBuilder.AppendLine(ListItemToOption(item));
  135. }
  136. TagBuilder builder = new TagBuilder("select") {
  137. InnerHtml = listItemBuilder.ToString()
  138. };
  139. builder.MergeAttributes(htmlAttributes);
  140. builder.MergeAttribute("name", name);
  141. builder.MergeAttribute("id", name);
  142. if (allowMultiple) {
  143. builder.MergeAttribute("multiple", "multiple");
  144. }
  145. return builder.ToString(TagRenderMode.Normal);
  146. }
  147. }
  148. }