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

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -