Geeks With Blogs
Scott Wojan DotRant BLOG

public static MvcHtmlString DropDownListFromEnumFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttribute = null)

{

    var type = typeof (TProperty);

    var addBlank = false;

    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))

    {

        type = type.GetGenericArguments()[0];

        addBlank = true;

    }

    var values = (from f in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)

                                    let attribute = f.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true).FirstOrDefault() as System.ComponentModel.DescriptionAttribute

                                    select new

                                    {

                                        Text = attribute == null ? f.Name : attribute.Description,

                                        Value = f.Name

                                    }).ToList();

    if (addBlank)

        values.Insert(0, new {Text = String.Empty, Value = String.Empty});

 

    return helper.DropDownListFor(expression,new SelectList(values, "Value", "Text"), htmlAttribute);

}

 

Will take…

public enum Salutation

{

    [Description("Mr.")]

    Mr = 100,

    [Description("Mr.")]

    Ms = 101,

    [Description("Mrs.")]

    Mrs = 102,

    [Description("Miss")]

    Miss = 103

}

And convert it to…

<select id="Suffix" name="Suffix">

  <option value="Junior">Junior</option>

  <option value="Senior">Senior</option>

  <option value="First">First</option>

  <option value="Second">Second</option>

  <option value="Third">Third</option>

  <option value="Fourth">Fourth</option>

  <option value="Fifth">Fifth</option>

</select>

Or if it’s a nullable property…

<select id="Suffix" name="Suffix">

  <option value=""></option>

  <option value="Junior">Junior</option>

  <option value="Senior">Senior</option>

  <option value="First">First</option>

  <option value="Second">Second</option>

  <option value="Third">Third</option>

  <option value="Fourth">Fourth</option>

  <option value="Fifth">Fifth</option>

</select>

 

 

Posted on Wednesday, March 13, 2013 5:07 PM | Back to top


Comments on this post: ASP.NET MVC Helper Method for Enums... supports override of text using System.ComponentModel.DescriptionAttribute

# re: ASP.NET MVC Helper Method for Enums... supports override of text using System.ComponentModel.DescriptionAttribute
Requesting Gravatar...
This is surely something that will be able to blow your mind and keep your head busy for quite a while. It is not an easy thing to crack but I am sure someone will find a way sooner or later. outsource medical billing
Left by Joe Turner on Feb 17, 2016 7:12 AM

# Amazing post
Requesting Gravatar...
Thanks for the nice blog.It is very useful for me.keep it up.Thanks for sharing such information with us. Host1Plus promo code

Left by Alex on Oct 21, 2016 1:18 AM

# affordable website hosting pakistan
Requesting Gravatar...
Thanx for sharing such useful post keep it up :)
affordable website hosting pakistan
Left by POTER on Dec 15, 2016 12:15 AM

# re: ASP.NET MVC Helper Method for Enums... supports override of text using System.ComponentModel.DescriptionAttribute
Requesting Gravatar...
Good way of telling, good post to take facts regarding my presentation subject matter, which i am going to deliver in my college
harvard case solutions
Left by John Martin on Apr 13, 2017 1:25 AM

# re: ASP.NET MVC Helper Method for Enums... supports override of text using System.ComponentModel.DescriptionAttribute
Requesting Gravatar...
Get the dissertation writing service students look for these days with the prime focus being creating a well researched and lively content on any topic.
Do My Programming Homework
Left by Alison Andrew on Apr 13, 2017 1:46 AM

# re: ASP.NET MVC Helper Method for Enums... supports override of text using System.ComponentModel.DescriptionAttribute
Requesting Gravatar...

Best Happy Father's Day 2017 Images, Photos, Wallpapers, Pics, Profile Pictures For Facebook Whatsapp.
The happy father's day messages is here with a great collection of Best Happy Father's Day 2017 Images,
Photos, Wallpapers, Pics, Profile Pictures For Facebook Whatsapp.happy fathers day
You can also share these father's day 2017 images, wallpapers, father's day 2017 profile pictures,
father's day facebook profile pictures, father's day wishing images anywhere on internet.
Left by karan on Jun 02, 2017 4:55 AM

Your comment:
 (will show your gravatar)


Copyright © Scott Wojan | Powered by: GeeksWithBlogs.net