473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MVC3 razor view dropdownlist from Enum

I am filling my dropdownlist with items from Enum.How can I get the index of enum items to dropdown list ?

Here is my Enum
Expand|Select|Wrap|Line Numbers
  1. public enum Colors{ red,blue,green,yellow,orange,white,black,Teal,Custom }
  2.  
Here is my dropdownlist
Expand|Select|Wrap|Line Numbers
  1.  @Html.DropDownList("SelectedColourId", Model.ColourList, "(Select one Color)")
Here is my ViewModel
Expand|Select|Wrap|Line Numbers
  1.  myPageViewModel:BasicViewModel
  2.     {
  3.  .....
  4.      public IEnumerable<SelectListItem> ColourList{ get; set; }
  5. .........
  6.     }
Here is how I get data from Enum
Expand|Select|Wrap|Line Numbers
  1. myPageViewModel.ColourList = Enum.GetNames(typeof(Colors))
  2.     .Select(c => new SelectListItem() { Text = c, Value = c })
  3.     .ToArray();
  4.  
Here I am able to populate my dropdownlist with colors

Expand|Select|Wrap|Line Numbers
  1. <select id="SelectedColourId" name="SelectedColourId"><option value="">(Select one Color)</option>
  2. <option value="red">red</option>
  3. <option value="Blue">Blue</option>
  4. <option value="Green">Green</option>
  5. <option value="Yellow">Yellow</option>
  6. --------
  7. --------
  8. </select>

But I need

Expand|Select|Wrap|Line Numbers
  1. <select id="SelectedColourId" name="SelectedColourId"><option value="0">(Select one Color)</option>
  2. <option value="1">red</option>
  3. <option value="2">Blue</option>
  4. <option value="3">Green</option>
  5. <option value="4">Yellow</option>
  6. --------
  7. --------
  8. </select>
Apr 30 '12 #1
2 4821
RhysW
70
in your enumeration you're setting both text and value to c, this could be the reason why the text and the value are the same
May 1 '12 #2
use the following code for enum drop down boxes:
Expand|Select|Wrap|Line Numbers
  1.    public enum UserStatus
  2.         {
  3.             New = 1,
  4.             Unverified = 2,
  5.             Active = 3,
  6.             Inactive = 4
  7.         }
View:

Expand|Select|Wrap|Line Numbers
  1.  @Html.DropDownListFor(model => model.Status, Enum.GetValues(typeof(Enum.UserStatus)).Cast<Enum.UserStatus>().Select(v => new SelectListItem
  2.                              {
  3.                                  Text = v.ToString().Replace("_", " "),
  4.                                  Value = ((int)v).ToString()
  5.                              }), "Select One")
Oct 1 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark Heimonen | last post by:
I haven't worked with enum types that much, and I wrote the following code to bind a list of enums to a dropdownlist. Using CType(.Parse(GetType(ManagePointClassLibrary.Utils.Log.EventType),...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
8
by: CK | last post by:
Hi All, Good morning. I had a quick question. I have a public Enum. During page load I want to loop thru the Enum and put those as items in an asp:dropdownList. Does anyone have any sample code to...
8
by: CK | last post by:
Hi All, I have a class and one property is an Enum. I am trying to persist the DropDownList.SelectedValue to the class property. I am using job.ExpRelated =...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
0
by: shapper | last post by:
Hello, I have an Enumeration as follows: Public Enum City NewYork London End Enum I have a function which returns a string for each City for different
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
5
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
he yall, does anyone know the best way to populate a dropdownlist from an enum list? thanks, rodchar
1
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following Enum I would like to display in an <asp:DropDownList> control. I have no problem getting the names to appear using the .GetNames method. Thus, None, Male and Female appear in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.