Selecting Model value inside an edit form for DropDownListFor()

4 posts, 0 answers
  1. Vesselin Obreshkov
    Vesselin Obreshkov avatar
    81 posts
    Member since:
    Jan 2010

    Posted 20 Jun 2012 Link to this post

    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
  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 21 Jun 2012 Link to this post

    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!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Vesselin Obreshkov
    Vesselin Obreshkov avatar
    81 posts
    Member since:
    Jan 2010

    Posted 21 Jun 2012 Link to this post

    Does the latest internal build (2012.1.615) or the new MVC Beta that should be released today have this fixed? 

    Thanks
  5. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 21 Jun 2012 Link to this post

    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!
Back to Top
UI for ASP.NET MVC is VS 2017 Ready