fork download
  1. public static class HTMLHelpExprettions
  2. {
  3. private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
  4. {
  5. Type realModelType = modelMetadata.ModelType;
  6.  
  7. Type underlyingType = Nullable.GetUnderlyingType(realModelType);
  8. if (underlyingType != null)
  9. {
  10. realModelType = underlyingType;
  11. }
  12. return realModelType;
  13. }
  14.  
  15. private static Type GetEnumTypeFromAttribute<TModel>(Expression<Func<TModel, int>> expression)
  16. {
  17. MemberInfo propertyInfo =
  18. ((expression.Body as MemberExpression).Member as MemberInfo);
  19. if (propertyInfo == null)
  20. throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
  21. EnumDataTypeAttribute attr = Attribute.GetCustomAttribute(propertyInfo,
  22. typeof(EnumDataTypeAttribute)) as EnumDataTypeAttribute;
  23. if (attr == null)
  24. throw new ArgumentException("式木で指定されたメンバーにはEnumDataTypeが指定されていません");
  25. return attr.EnumType;
  26. }
  27. private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = "", Value = "" } };
  28.  
  29. //エイリアス
  30. public static string DisplayEnumFor<TModel>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, int>> expression)
  31. {
  32. return GetEnumDescription(expression.Compile()(htmlHelper.ViewData.Model),GetEnumTypeFromAttribute(expression));
  33. }
  34.  
  35. private static string GetEnumDescription(int enumValue, Type enumType)
  36. {
  37. FieldInfo fi = enumType.GetField(enumType.GetEnumName(enumValue));
  38.  
  39. DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  40.  
  41.  
  42. if ((attributes != null) && (attributes.Length > 0))
  43. return attributes[0].Description;
  44. else
  45. return enumType.GetEnumName(enumValue);
  46. }
  47.  
  48. /// <summary>
  49. /// EnumDataTypeAttributeで情報が付け加えられたint型のプロパティーをドロップダウンに変換します。
  50. /// </summary>
  51. /// <typeparam name="TModel"></typeparam>
  52. /// <param name="htmlHelper"></param>
  53. /// <param name="expression"></param>
  54. /// <param name="model"></param>
  55. /// <param name="htmlAttributes"></param>
  56. /// <returns></returns>
  57. public static MvcHtmlString EnumDropDownListFor<TModel>(
  58. this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, int>> expression, object htmlAttributes = null)
  59. {
  60. ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
  61. Type enumType = GetEnumTypeFromAttribute(expression);
  62. IEnumerable<int> values = Enum.GetValues(enumType).Cast<int>();
  63.  
  64. IEnumerable<SelectListItem> items =
  65. values.Select(value=>new SelectListItem
  66. {
  67. Text = GetEnumDescription(value,enumType),
  68. Value = value.ToString(),
  69. Selected = value.Equals(metadata.Model)
  70. });
  71.  
  72. // If the enum is nullable, add an 'empty' item to the collection
  73. if (metadata.IsNullableValueType)
  74. items = SingleEmptyItem.Concat(items);
  75.  
  76. return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
  77. }
  78. }
  79.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty