Skip to content

Instantly share code, notes, and snippets.

@MMF
Last active March 5, 2017 08:37
Show Gist options
  • Save MMF/d9fdc9e48f91a0cd344d3196b278a031 to your computer and use it in GitHub Desktop.
Save MMF/d9fdc9e48f91a0cd344d3196b278a031 to your computer and use it in GitHub Desktop.
EnumUtil Class For ASP.NET MVC - Generates HTML Select, HTML Radio From Enum Code
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web;
namespace SolutionName.Helpers
{
public static class EnumUtil
{
/*
How to use?
if you have enum like
public enum LeaveStatus
{
[Display(Name = "Accepted Leave!")]
Accepted = 1,
[Display(Name = "Refused By Manager")]
Refused = 2,
[Display(Name = "Waiting Manager Approval")]
Pending = 3
}
- Display the name of enum item
LeaveStatus.Pending.GetDisplayName()
- convert to dictionary
foreach (KeyValuePair<int, string> item in EnumUtil.AsDictionary( typeof(LeaveStatus) ))
{
// use item.Key, item.Value to access current dictionary items
}
- To generate Html Select Code
@EnumUtil.HtmlSelect( typeof(LeaveStatus), "fieldName" )
- To generate Html Radio Code
@EnumUtil.HtmlRadio( typeof(LeaveStatus), "fieldName" )
*/
// Get DisplayName attribute of the Enum
public static string GetDisplayName(this Enum enumVal)
{
var displayAttr = enumVal.GetType()
.GetMember(enumVal.ToString())
.First()
.GetCustomAttributes(false)
.OfType<DisplayAttribute>()
.FirstOrDefault();
string result = string.Empty;
if (displayAttr != null)
{
result = displayAttr.Name;
}
return result;
}
/// <summary>
/// Exports Enum as a Dictionary Val => DisplayName
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <returns></returns>
public static Dictionary<int, string> AsDictionary(Type enumType)
{
Dictionary<int, string> dt = new Dictionary<int, string>();
foreach (Enum item in Enum.GetValues(enumType))
{
dt.Add(Convert.ToInt32(item), item.GetDisplayName());
}
return dt;
}
/// <summary>
/// generates HTML select code for Enum Type
/// </summary>
/// <typeparam name="T">typeof(EnumName)</typeparam>
/// <param name="inputName">the name of the HTML select</param>
/// <param name="id">ID for Javascript of css usage</param>
/// <returns></returns>
public static IHtmlString HtmlSelect(Type enumType, string inputName, string id = "", string cssClass="form-control")
{
StringBuilder builder = new StringBuilder();
builder.Append($"<select class='{cssClass}' name='{inputName}' id='{id}'>");
foreach (KeyValuePair<int, string> item in EnumUtil.AsDictionary(enumType))
{
builder.Append($"<option value='{item.Key}'>{item.Value}</option>");
}
builder.Append("</select>");
return new HtmlString(builder.ToString());
}
/// <summary>
/// generates HTML radio List code for Enum Type
/// </summary>
/// <param name="enumType">typeof(EnumName)</param>
/// <param name="inputName">the name of the input</param>
/// <param name="inputClass">css class name</param>
/// <returns></returns>
public static IHtmlString HtmlRadio(Type enumType, string inputName, int defaultValue=0, string inputClass="")
{
StringBuilder builder = new StringBuilder();
bool firstItemNotChecked = true;
foreach (KeyValuePair<int, string> item in EnumUtil.AsDictionary(enumType))
{
builder.Append("<label>");
builder.Append($"<input type='radio' name='{inputName}' id='{inputName}_{item.Key}' class='{inputClass}' value='{item.Key}' ");
if (firstItemNotChecked && defaultValue == 0)
{
builder.Append("checked='checked' ");
firstItemNotChecked = false;
}
// check the radio with default value
if (defaultValue != 0 && item.Key == defaultValue)
{
builder.Append("checked='checked' ");
}
builder.Append($"/> {item.Value}</label>");
}
return new HtmlString(builder.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment