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

[Solved] DropDownListFor SelectedIndex

4 Answers 251 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.
cp
Top achievements
Rank 1
cp asked on 28 Mar 2011, 09:11 AM
Hi,
I'm having a little trouble with a DropDownListFor where I'm not able to change the selected item. I've tried both selecting an item inside the BindTo method as well as using SelectedIndex and neither seem to change the selection.

This is the code I'm using:
<%= Html.Telerik().DropDownListFor(model => model.SomeObject.SomeProperty)
              .HtmlAttributes(new { style = "width: 160px;" })
              .BindTo(new SelectList(Model.Statuses, "Text", "Value", Model.SomeObject.SomeProperty))
%>

If I change it to a DropDownList the selected item changes as expected.

Any help is appreciated.

Thanks,

4 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 28 Mar 2011, 12:46 PM
Hello Claudiu,

DropDownList component searches for pair <key-value> in the ViewData. The key should be same as the component Name. DDL will try to find DropDownItem which has same value and will select it. If the DDL succeed with this actions then the SelectedIndex is ignored. If you need to select item using SelectedIndex method, then component should not be named as a key in the ViewData dictionary. 

If the problem still exists I will suggest you to send us a simple project which reproduces the depicted issue. Thus I will be able to observe the problem locally and advice you further.

Regards,
Georgi Krustev
the Telerik team
0
cp
Top achievements
Rank 1
answered on 29 Mar 2011, 04:14 PM
Thanks Georgi.

I will see if I can pull out some of the code to duplicate the problem I'm having (unless you already have a sample with a dropdownlistfor inside an ajax.beginform). I have a few other helpers in that partial view and they all work fine, I'm just not able to get the selected item to display inside the dropdownlist on postback. The rebinding works as well since I modified the list on postback and I'm able to see the new values.
0
Hristo Germanov
Telerik team
answered on 30 Mar 2011, 12:26 PM
Hello Claudiu,

I can't find any problem with a DropDownListFor inside an Ajax.BeginForm.

I have attached a simple project which show how to preselect item inside an Ajax.BeginForm.

If this does not help I would ask you to provide a sample application which we can test locally.

Kind regards,
Hristo Germanov
the Telerik team
0
Binkal
Top achievements
Rank 1
answered on 16 Jul 2012, 08:29 AM

Hi,

I have faced same problem which have described above and I had download attached code and I have review this code but we want functionality like when we are using tab key from key board then control goes to drop down but drop down is not highlighted.

So please guide us for above point.

Thank you.

Binkal Patel



Tags
ComboBox
Asked by
cp
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
cp
Top achievements
Rank 1
Hristo Germanov
Telerik team
Binkal
Top achievements
Rank 1
Share this question
or