Search code examples
razortelerik-mvcasp.net-mvc-3-areas

How to make a Telerik Combobox display a value from a field in the model that corresponds to a value in the SelectList?


I am using a Telerik MVC control (combobox) and I am having trouble getting the value from the model to highlight as selected when the view loads.

             <tr>
            <td class="editor-label">
               @Html.LabelFor(model => model.CategoryDescription)
            </td>
            <td class="editor-field">
                @Html.DisplayFor(model => model.CategoryDescription)
            </td>
            <td>
                @(Html.Telerik().ComboBoxFor(model => model.CategoryDescription)
                                    .BindTo((SelectList)(Model.CategoryDescriptionList))//(SelectList)(ViewBag.CategoriesList))
                                    .HtmlAttributes(new { style = "width:350px; white-space:nowrap;" })
                                    .HighlightFirstMatch(true)
                                    .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                                    .ClientEvents(events => events.OnClose("ComboBox_OnClose"))
                  )
                @Html.ValidationMessageFor(model => model.CategoryDescription)
            </td>
        </tr>

I want the value in Model.CategoryDescription to show up in the Combobox as the selected entry when the page loads. So the question is how to do that.

Answered. I was able to set the SelectedIndex on the control when I built the selectList in my model and used its location for the control.


Solution

  • I was able to set the SelectedIndex on the control when I built the selectList in my model and used its location for the control.