Created
October 5, 2016 17:30
-
-
Save csdear/c22897bf57bb2b09dd108da59bacb4fb to your computer and use it in GitHub Desktop.
An enum helper class
IsAny() : extension method to check for any null enums being passed. e.g. usage : <!--new tickers-->@if (Model.NewsHeadlines.IsAny()) was used to prevent area from crashing N2 website in case any of the newsheadlines were not configured which would pass in nulls, crashing the site. enum dropdownlistfor method usage : @Html.E…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Linq.Expressions; | |
using System.Web.Mvc.Html; | |
using System.ComponentModel; | |
using System.Web.Mvc; | |
using System.Reflection; | |
namespace Vulcraft.Web.Helpers.Extensions | |
{ | |
public static class EnumExtensions | |
{ | |
public static bool IsAny<T>(this IEnumerable<T> data) | |
{ | |
return data != null && data.Any(); | |
} | |
private static Type GetNonNullableModelType(ModelMetadata modelMetadata) | |
{ | |
Type realModelType = modelMetadata.ModelType; | |
Type underlyingType = Nullable.GetUnderlyingType(realModelType); | |
if (underlyingType != null) | |
{ | |
realModelType = underlyingType; | |
} | |
return realModelType; | |
} | |
private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = "", Value = "" } }; | |
public static string GetEnumDescription<TEnum>(TEnum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if ((attributes != null) && (attributes.Length > 0)) | |
return attributes[0].Description; | |
else | |
return value.ToString(); | |
} | |
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression) | |
{ | |
return EnumDropDownListFor(htmlHelper, expression, null); | |
} | |
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes) | |
{ | |
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); | |
Type enumType = GetNonNullableModelType(metadata); | |
IEnumerable<TEnum> values = Enum.GetValues(enumType).Cast<TEnum>(); | |
IEnumerable<SelectListItem> items = from value in values | |
select new SelectListItem | |
{ | |
Text = GetEnumDescription(value), | |
Value = value.ToString(), | |
Selected = value.Equals(metadata.Model) | |
}; | |
// If the enum is nullable, add an 'empty' item to the collection | |
if (metadata.IsNullableValueType) | |
items = SingleEmptyItem.Concat(items); | |
return htmlHelper.DropDownListFor(expression, items, htmlAttributes); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment