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

4 posts, 0 answers
  1. Phillip
    Phillip avatar
    6 posts
    Member since:
    Jul 2014

    Posted 30 Apr 2015 Link to this post

    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.

  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 04 May 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Phillip
    Phillip avatar
    6 posts
    Member since:
    Jul 2014

    Posted 04 May 2015 in reply to Georgi Krustev Link to this post

    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.

  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 06 May 2015 Link to this post

    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!
     
Back to Top
Kendo UI is VS 2017 Ready