This is a migrated thread and some comments may be shown as answers.

Selected value on dropdownlist

5 Answers 292 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Duncan
Top achievements
Rank 1
Duncan asked on 10 Aug 2011, 11:01 AM
Hi,

I have a dropdownlist which I'm populating it with a list of enums, and I am passing in the enum I require to be the selected item, this all seems to be set, and the EnumHelper is returning all the correct values, except it doesn't set the selected item, and just sets to the first item in the drop down list.

Any thoughts?

 @(Html.Telerik().DropDownList().Name("MyDropDown")
                .BindTo(new SelectList(EnumHelper.EnumDropDownList<PropertyBased>((int)PropertyBased.Yes)
, "Value""Text""Selected")))


public static IEnumerable<SelectListItem> EnumDropDownList<T>(int selectedValue)
        {
            var items = new List<SelectListItem>();
 
            foreach (int data in Enum.GetValues(typeof(T)))
            {
                items.Add(new SelectListItem
                {
                    //populate a drop down with values, separating camelcase text with a space.
                    Text = Enum.GetName(typeof(T), data).SpaceCamelCase(),
                    Value = data.ToString(),
                    Selected = (data.Equals(selectedValue))
                });
            }
 
            return items;
	}


5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 11 Aug 2011, 10:28 AM
Hi Duncan,

 Does this work with the regular Html.DropDownList? According to MSDN the third parameter of the SelectList constructor specifies the selected value rather than the selected property name.

Regards,

Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Duncan
Top achievements
Rank 1
answered on 11 Aug 2011, 11:23 AM
Hi, 

Thanks for the reply, I tried this code but still not working, this assumes I understood the html dropdownlist correctly of course,

 @{
     var items = EnumHelper.EnumDropDownList<PropertyBased>((int)PropertyBased.Yes, false);
 }
              
 @Html.DropDownList("Test"new SelectList(items, "Value""Text",items.Where(a=>a.Selected==true)))


this still defaults to the first item in the list, I'm just wondering whether it is being overridden some where.


Duncan
0
Accepted
Atanas Korchev
Telerik team
answered on 11 Aug 2011, 11:28 AM
Hi Duncan,

 

items.Where(a=>a.Selected==true)

will return a collection of selected items not the selected value. You need to specify the selected value instead.

You can try passing the IEnumerable<SelectListItem> instead of creating a SelectList out of it (which is basically another IEnumerable<SelectListItem>).

For further info you can check here and here.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Duncan
Top achievements
Rank 1
answered on 11 Aug 2011, 02:28 PM
Hi

This seems to be working now, which now does make sense 

Thanks for your help.

@{
  var items = EnumHelper.EnumDropDownList<PropertyBased>((int)PropertyBased.Yes, false);
}
           
@Html.DropDownList("Test",items)
0
Atanas Korchev
Telerik team
answered on 11 Aug 2011, 02:37 PM
Hi Duncan,

 Once you got it to work with Html.DropDownList you can now use Html.Telerik().DropDownList() like this:

@(Html.Telerik().DropDownList().Name("MyDropDown")
  .BindTo(items)
)

All the best,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Tags
ComboBox
Asked by
Duncan
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Duncan
Top achievements
Rank 1
Share this question
or