How can I bind an enum to a model-field of the type integer?
I tried some extension methods, but they didnt do a good job, cause all method required the model field to be the given enum-type...
Here is my source (Model and Enum):
Model:
public class Einheit
{
public Einheit()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public short TypeOfSomething { get; set; }
public long Verwendungszweck { get; set; }
}
Enum:
public enum Einheitsart
{
Type1 = 0,
Type2 = 1,
Type3 = 2,
Type4 = 3,
Type5 = 4,
Type6 = 5
}
I want to have the Values going from 0-6 (To be able to save the Integer in my Model), but the DropDownList should show the Text "Type1" to "Type6"...
The problem I have is converting the enum to a working SelectList.
Thank you!
Try the following helper I created.
Full details can be seen on my blog:
http://www.ninjanye.co.uk/2012/01/creating-dropdown-list-from-enum-in.html
http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23
public static class EnumHelper
{
//Creates a SelectList for a nullable enum value
public static SelectList SelectListFor<T>(T? selected)
where T : struct
{
return selected == null ? SelectListFor<T>()
: SelectListFor(selected.Value);
}
//Creates a SelectList for an enum type
public static SelectList SelectListFor<T>() where T : struct
{
Type t = typeof (T);
if (t.IsEnum)
{
var values = Enum.GetValues(typeof(T)).Cast<enum>()
.Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });
return new SelectList(values, "Id", "Name");
}
return null;
}
//Creates a SelectList for an enum value
public static SelectList SelectListFor<T>(T selected) where T : struct
{
Type t = typeof(T);
if (t.IsEnum)
{
var values = Enum.GetValues(t).Cast<Enum>()
.Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });
return new SelectList(values, "Id", "Name", Convert.ToInt32(selected));
}
return null;
}
// Get the value of the description attribute if the
// enum has one, otherwise use the value.
public static string GetDescription<TEnum>(this TEnum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
}
return value.ToString();
}
}
To use it simply use the following code:
//If you don't have an enum value use the type
ViewBag.DropDownList = EnumHelper.SelectListFor<MyEnum>();
//If you do have an enum value use the value (the value will be marked as selected)
ViewBag.DropDownList = EnumHelper.SelectListFor(myEnumValue);
Note: by adding a description attribute to your enum, it will use that as the display text for the drop down:
public enum Einheitsart
{
Type1 = 0,
[Description("2nd Type")]
Type2 = 1,
Type3 = 2,
Type4 = 3,
Type5 = 4,
Type6 = 5
}
You can enumerate over all enum values and create SelectListItems for each of them. This should work:
var selectList = new List<SelectListItem>();
foreach(Einheitsart art in Enum.GetValues(typeof(Einheitsart)))
{
selectList.Add(new SelectListItem() { Value = (int)art, Text = art.ToString() })
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.