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

[Solved] DropDownList with NULL value (option label)

2 Answers 202 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.
Saud Almutairi
Top achievements
Rank 1
Saud Almutairi asked on 07 Jul 2011, 11:08 AM
using Telerik.Web.MVC 2011.414.340


trying to have a dropdownlist bound to list and this field is optional

i do it in Html.DropDownList while setting the "optionLabel" parameter

how do i do it in Telerik.DropDownListFor

-i tried setting selected index to -1 but i got the following error

"index" cannot be negative.
Parameter name: index

2 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 07 Jul 2011, 11:37 AM
Hello Saud,

 
The DropDownListFor component currently does not support "optionLabel" functionality of the Html.DropDownListFor. Nevertheless you can easily implement it with adding SelectListItem at the first position of the list. Check this code snippet:

<%
        var list = new SelectList(Model.Products, "ProductID", "ProductName").ToList();
        list.Insert(0, new SelectListItem { Text = "Select option...", Value = "-1" });
    %>
 
    <%= Html.Telerik().DropDownList()
            .Name("DropDownList")
            .SelectedIndex(0)
            .BindTo(list)
    %>
Please note that if you want to use ToList() extension method in the ASPX page, then you will need to register System.Linq namespace in the web.config.

Regards,
Georgi Krustev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Saud Almutairi
Top achievements
Rank 1
answered on 07 Jul 2011, 06:07 PM
works great thanks.

Tags
ComboBox
Asked by
Saud Almutairi
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Saud Almutairi
Top achievements
Rank 1
Share this question
or