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

Why SelectedIndex(2) always return the 0 index value on the dropdownlist

3 Answers 578 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 30 Apr 2015, 02:40 PM

I have the following:

@(Html.Kendo().DropDownListFor(model => model.RecurringTypeID)
    .SelectedIndex(0) //Select 1st item.
    .DataTextField("Text")
    .DataValueField("Value")
    .BindTo(Model.RecurringType)
    .HtmlAttributes(new { style = "font-size:11px;width: 175px;" })
    .Events(e =>
        {
            e.Change("OnChangeRecurringType");
        })
)

Why  .SelectedIndex(0), .SelectedIndex(1), or .SelectedIndex(2) always return the 1st value on the dropdownlist.

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 04 May 2015, 09:12 AM
Hello Phillip,

The described behavior is actually expected, because the widget will ignore the SelectedIndex option if value is defined, which is exactly the case when DropDownListFor extension is used. Basically, the DropDownListFor method will retrieve the name and value from the specified model. Hence it will use the model value instead of the index, because the defined value has a higher priority (check the attached project for better understanding).

If you would like to select a specific item, you will need to set the proper item value. 

Regards,
Georgi Krustev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Phillip
Top achievements
Rank 1
answered on 04 May 2015, 12:42 PM

Hi Georgi,

How do I set the default value(not the 1st item) when the value from the model is not set? If the value from the model is set, I don't want the default value.

Thanks.

0
Atanas Korchev
Telerik team
answered on 06 May 2015, 11:26 AM

Hello Phillip,

In that case you should not use DropDownListFor. Use Html.Kendo().DropDownList and set the SelectedIndex option.

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
DropDownList
Asked by
Phillip
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Phillip
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or