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

Selecting Model value inside an edit form for DropDownListFor()

3 Answers 346 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Vesselin Obreshkov
Top achievements
Rank 2
Vesselin Obreshkov asked on 20 Jun 2012, 04:30 PM
How do we go about selecting the value of a server-bound DropDownListFor() in an edit form? Example:

@(Html.Kendo().DropDownListFor(m => m.Country)
		.BindTo(ViewBag.Countries)
		.DataValueField("CountryCode")
		.DataTextField("CountryName")
		.OptionLabel("Select Country"))

ViewBag.Countries contains a List<Country>. Country object has a CountryCode and CountryName (strings). The model I'm passing in has a Country field that holds the exact value that's bound to DataValueField("CountryCode"). Nothing gets pre-selected when I open the form (but the list of countries is there). The SelectedIndex() property is useless to me in this instance. Digging through the DropDownListBuilder class the only thing I saw was a Value() method that I'm not sure what it does but giving it the value I want selected doesn't do anything either.

I can't give BindTo() a SelectList() like I used to with the Telerik extensions (unless I'm doing something wrong).

Thanks for any help

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 21 Jun 2012, 08:45 AM
Hello Vesselin,

In the current beta (2012.2.531) the DropDownList does not render the set value. This issue is already addressed and the fix will be included in the next official release. For now you can overcome this problem like this:

@(Html.Kendo().DropDownListFor(m => m.Country)
        .BindTo(ViewBag.Countries)
                .HtmlAttributes(new { value = Model.Country })
        .DataValueField("CountryCode")
        .DataTextField("CountryName")
        .OptionLabel("Select Country"))

Kind regards,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Vesselin Obreshkov
Top achievements
Rank 2
answered on 21 Jun 2012, 01:11 PM
Does the latest internal build (2012.1.615) or the new MVC Beta that should be released today have this fixed? 

Thanks
0
Georgi Krustev
Telerik team
answered on 21 Jun 2012, 02:23 PM
Hello,

Latest internal build does not have MVC in it. The Kendo UI for ASP.NET MVC, which will be released today, will have this fixed.

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