asp.net mvc - C# MVC: DropDownListFor bind a enum to a integer model-field -
how can bind enum model-field of type integer?
i tried extension methods, didnt job, cause method required model field given enum-type...
here source (model , 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 have values going 0-6 (to able save integer in model), dropdownlist should show text "type1" "type6"...
the problem have converting enum working selectlist.
thank you!
you can enumerate on enum values , create selectlistitems each of them. 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() }) }
Comments
Post a Comment