neel
neel

Reputation: 5303

Issues in enum values in MVC4

hi all i am try to make a dropdown list using enum values. I have a select field in to my html page like,

   <select name="Mem_BloodGr" >
   <option value="A+">A+</option><option value="A-">A-</option>
   <option value="B+">B+</option><option value="B-">B-</option>
   <option value="O+">O+</option><option value="O-">O-</option>
   <option value="AB+">AB+</option><option value="AB-">AB-</option>
   </select>

This is repeated in many places in my web page. So i am try to generate dropdown list using enum values

namespace .....Models
{
   public class MemberData
   {
    public int Id { get; set; }
    public string Mem_NA { get; set; }
    ........
    public BloodGroup Mem_BloodGr { get; set; }
   }

  public enum BloodGroup
   {
    A+,                //// **error shows here like, "} expected"**
    A-,
    B-,
    B+

   }
 }

but i got an error when adding enum values . Can anybody please help me.And is that the right way to creating this type dropdown list or any other easy ways in MVC4???

Upvotes: 1

Views: 380

Answers (2)

H&#229;kan Fahlstedt
H&#229;kan Fahlstedt

Reputation: 2095

You can't have + and - in enum values, just letters and numbers. Use APlus or something instead. You could use enums as a source for a dropdown, but I would recommend a list of object that has a name instead. This way you have more control over what is displayed and you are able to use any character in the names. Use something like this to fill the dropdown with:

public class BloodGroup
{
  public string Name {get; set; }
  // other properties
}

Then you can create a list of BloodGroup-objects that you can reuse in your views:

var BloodGroupList = new List<BloodGroup> { new BloodGroup { Name = "A+"}, new BloodGroup { Name = "A-"}, ... };

And in your views:

@Html.DropDownList("BloodGroups", BloodGroupList.Select(b => new SelectListItem { Name = b.Name, Value = b.Name }))

Upvotes: 1

Roman Pushkin
Roman Pushkin

Reputation: 6099

Here is my approach for dropdown for enums:

Make our own attribute:

public class EnumDescription : Attribute
{
    public string Text { get; private set; }

    public EnumDescription(string text)
    {
        this.Text = text;
    }
}

Make a helper class:

public static class SelectListExt
{
    public static SelectList ToSelectList(Type enumType)
    {
        return ToSelectList(enumType, String.Empty);
    }

    public static SelectList ToSelectList(Type enumType, string selectedItem)
    {
        List<SelectListItem> items = new List<SelectListItem>();

        foreach (var item in Enum.GetValues(enumType))
        {
            FieldInfo fi = enumType.GetField(item.ToString());
            var attribute = fi.GetCustomAttributes(typeof(EnumDescription), true).FirstOrDefault();
            var title = attribute == null ? item.ToString() : ((EnumDescription)attribute).Text;

            // uncomment to skip enums without attributes
            //if (attribute == null)
            //    continue;

            var listItem = new SelectListItem
            {
                Value = ((int)item).ToString(),
                Text = title,
                Selected = selectedItem == ((int)item).ToString()
            };
            items.Add(listItem);
        }

        return new SelectList(items, "Value", "Text", selectedItem);
    }
}

Enum itself:

public enum DaylightSavingTime
{
    [EnumDescription("Detect automatically")]
    Auto = 0,
    [EnumDescription("DST always on")]
    AlwaysOn = 1,
    [EnumDescription("DST always off")]
    AlwaysOff = 2
}

You can put everything you want in EnumDescription

Usage:

@Html.DropDownList(
    "dst",
    SelectListExt.ToSelectList(typeof(DaylightSavingTime)),
    new {
        id = "dst",
        data_bind = "value: Dst"
    })

Note: data_bind is transformed to "data-bind". It's useful is you use Knockout.js

You can omit the latest new {} block

Upvotes: 1

Related Questions