Drop-Down List Enum ASP.NET MVC Using AttributeDescription Field -


based on post: how create dropdownlist enum in asp.net mvc?

i want exact same thing, except using attributedescription field enum, example:

[descriptionattribute("1 star")] onestar = 1, [descriptionattribute("2 stars")] twostar = 2, [descriptionattribute("3 stars")] threestar = 3, [descriptionattribute("4 stars")] fourstar = 4 

the solution given in prior link show "onestar" in text field of drop down, whereas i'd want see "1 star". i've seen few posts relating this, solutions quite verbose.

you may try among lines:

public static selectlist toselectlist<tenum>(this tenum enumobj) {     var enumtype = typeof(tenum);     var fields = enumtype.getfields(         bindingflags.static | bindingflags.getfield | bindingflags.public     );     var values = enum.getvalues(enumtype).oftype<tenum>();     var items =          value in values         field in fields         let descriptionattribute = field             .getcustomattributes(                 typeof(descriptionattribute), true             )             .oftype<descriptionattribute>()             .firstordefault()         let description = (descriptionattribute != null)             ? descriptionattribute.description              : value.tostring()         value.tostring() == field.name         select new { id = value, name = description };     return new selectlist(items, "id", "name", enumobj); } 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -