PageRenderTime 79ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MvcFutures/Mvc/RadioExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 91 lines | 76 code | 12 blank | 3 comment | 12 complexity | 867e9ee15033c0c4538907d8329b181b MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Mvc.Html;
  10. using System.Web.Routing;
  11. using Microsoft.Web.Mvc.Resources;
  12. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  13. public static class RadioListExtensions {
  14. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name) {
  15. return RadioButtonList(htmlHelper, name, (IDictionary<string, object>)null);
  16. }
  17. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name, object htmlAttributes) {
  18. return RadioButtonList(htmlHelper, name, new RouteValueDictionary(htmlAttributes));
  19. }
  20. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {
  21. SelectList selectList = htmlHelper.GetSelectData(name);
  22. return htmlHelper.RadioButtonListInternal(name, selectList, true /* usedViewData */, htmlAttributes);
  23. }
  24. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name, SelectList selectList) {
  25. return RadioButtonList(htmlHelper, name, selectList, (IDictionary<string, object>)null);
  26. }
  27. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name, SelectList selectList, object htmlAttributes) {
  28. return RadioButtonList(htmlHelper, name, selectList, new RouteValueDictionary(htmlAttributes));
  29. }
  30. public static string[] RadioButtonList(this HtmlHelper htmlHelper, string name, SelectList selectList, IDictionary<string, object> htmlAttributes) {
  31. return htmlHelper.RadioButtonListInternal(name, selectList, false /* usedViewData */, htmlAttributes);
  32. }
  33. private static SelectList GetSelectData(this HtmlHelper htmlHelper, string name) {
  34. object o = null;
  35. if (htmlHelper.ViewData != null) {
  36. o = htmlHelper.ViewData.Eval(name);
  37. }
  38. if (o == null) {
  39. throw new InvalidOperationException(
  40. String.Format(
  41. CultureInfo.CurrentUICulture,
  42. MvcResources.HtmlHelper_MissingSelectData,
  43. name,
  44. typeof(SelectList)));
  45. }
  46. SelectList selectList = o as SelectList;
  47. if (selectList == null) {
  48. throw new InvalidOperationException(
  49. String.Format(
  50. CultureInfo.CurrentUICulture,
  51. MvcResources.HtmlHelper_WrongSelectDataType,
  52. name,
  53. o.GetType().FullName,
  54. typeof(SelectList)));
  55. }
  56. return selectList;
  57. }
  58. private static string[] RadioButtonListInternal(this HtmlHelper htmlHelper, string name, SelectList selectList, bool usedViewData, IDictionary<string, object> htmlAttributes) {
  59. if (String.IsNullOrEmpty(name)) {
  60. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
  61. }
  62. if (selectList == null) {
  63. throw new ArgumentNullException("selectList");
  64. }
  65. // If we haven't already used ViewData to get the entire list of items then we need to
  66. // use the ViewData-supplied value before using the parameter-supplied value.
  67. if (!usedViewData) {
  68. object defaultValue = htmlHelper.ViewData.Eval(name);
  69. if (defaultValue != null) {
  70. selectList = new SelectList(selectList.Items, selectList.DataValueField, selectList.DataTextField, defaultValue);
  71. }
  72. }
  73. // Convert each ListItem to an <option> tag
  74. IList<ListItem> listItems = selectList.GetListItems();
  75. IEnumerable<string> radioButtons = listItems.Select<ListItem, string>(item => htmlHelper.RadioButton(name, item.Value, item.Selected, htmlAttributes));
  76. return radioButtons.ToArray();
  77. }
  78. }
  79. }