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

Kendo MVC Dropdown list settings initial value not working

10 Answers 3271 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 03 Jun 2012, 12:42 PM
I am using the following code in my view.

@Html.Kendo().DropDownListFor(x => x.PasswordStrength).BindTo(Model.AvailablePasswordStrengths).DataTextField("Text").DataValueField("Value").Value(Model.PasswordStrength.ToString())

Notice that I am settings the "value" of the drop down list. The outputed js doesn't have the initial selected value.

jQuery(function(){jQuery("#PasswordStrength").kendoDropDownList({dataSource:[{"Selected":false,"Text":"Blank","Value":"0"},{"Selected":false,"Text":"Very weak","Value":"1"},{"Selected":false,"Text":"Weak","Value":"2"},{"Selected":true,"Text":"Medium","Value":"3"},{"Selected":false,"Text":"Strong","Value":"4"},{"Selected":false,"Text":"Very strong","Value":"5"}],dataTextField:"Text",dataValueField:"Value"});});

EDIT: After looking with reflector, I found DropDownItemFactory and DropDownItemBuilder, and I couldn't find an usages of them. Also, It seems like the "Value" of the builder isn't being used either. I'm confused. I know this is beta, but beta implies "we are done, but needs more testing," and not "We are not done implementing classes but here is what we got so far.". This is a BETA product and not a preview. The two are different.

10 Answers, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 17 Jun 2012, 12:48 PM
Did you ever get this working? Because I have exactly the same problem. I am trying to bind a DropDownList to a property of my model and the initial value just is not getting set correctly. I've wasted a good few hours on this and documentation appears to be rather scant.

With the old Telerik MVC extensions it appeared to be enough to just use DropDownListFor, BindTo and a SelectList with the available values. The model was bound automatically and the initial value set to that of the model. However, with Kendo this isn't happening.

How to set the initial value so that it matches that of the model?
0
Paul
Top achievements
Rank 1
answered on 17 Jun 2012, 02:28 PM
I have a workaround until they release a fix.

public static DropDownListBuilder BindToSelectList(this DropDownListBuilder builder, IList<SelectListItem> selectList, object selectedValue = null)
        {
            var selectedIndex = selectList.IndexOf(selectList.SingleOrDefault(x =>
            {
                if (x.Selected)
                    return true;
 
                if (selectedValue == null)
                    return x.Value == string.Empty;
 
                if (x.Value.Equals(selectedValue.ToString(), StringComparison.InvariantCultureIgnoreCase))
                    return true;
 
                return false;
            }));
 
            if (selectedIndex < 0)
            {
                selectedIndex = 0;
            }
 
            return builder.BindTo(selectList).DataTextField("Text").DataValueField("Value").SelectedIndex(selectedIndex);
        }
0
Matt
Top achievements
Rank 1
answered on 17 Jun 2012, 02:33 PM
Hi Paul,

Thanks for taking the time to come back and post your solution to this thread. Your extension method looks like it could save me quite a headache.

Could somebody from the Kendo team please confirm either that this is a bug which will be addressed in the official release or alternatively that I'm going about this all wrong and enlighten me on the correct way to approach this?
0
Georgi Krustev
Telerik team
answered on 18 Jun 2012, 10:13 AM
Hi guys,

 
This is a known issue, which is already fixed. We did not render the value of the DropDownList. In the next version the rendered input element will have a value attribute. You can overcome this limitation setting the attribute manually:

@(Html.Kendo().DropDownListFor(m => m.CustomerID)
      .BindTo(Model.Customers)
      .DataTextField("Text")
      .DataValueField("Value")
      .HtmlAttributes(new { value = "AROUT" })
)

I have attached a sample project.

All the best,
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
phil
Top achievements
Rank 1
answered on 29 Aug 2012, 10:40 PM
Georgi, is which release will this (or is this already) fixed?

In any case, I made a version based on Paul's workaround that wraps DropDownListFor when using enum properties in a slightly more concise way:

public static DropDownListBuilder EnumDropDownListFor<TModel, TEnum>(
    this WidgetFactory<TModel> widgetFactory,
    Expression<Func<TModel, TEnum>> selectedValue,
    TEnum value)
{
    var items = Enum.GetValues(typeof(TEnum))
                    .Cast<TEnum>()
                    .Select((x,i) => new {
                                Name = Enum.GetName(
typeof(TEnum), x),
                                Value = x,
                                Index = i })
                    .ToArray();
    var selectedItem = items.SingleOrDefault(x => x.Value.Equals(value));
    var selectedIndex = (null == selectedItem) ? 0 : (selectedItem.Index + 1);
 
    return widgetFactory.DropDownListFor<TEnum>(selectedValue)
                        .BindTo(items)
                        .SelectedIndex(selectedIndex)
                        .DataTextField("Name")
                        .DataValueField("Value");
}
0
Georgi Krustev
Telerik team
answered on 30 Aug 2012, 07:15 AM
Hello,

 
This was fixed in the 2012.2.710 (official Q2 release).

All the best,
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
Stephen
Top achievements
Rank 1
answered on 27 Sep 2012, 06:47 PM
So it seems that binding with a SelectList with a Selected value set is not going to set the selected index automatically?  Is there a way to set the selected value based on the selected value in the model?
0
Mattias
Top achievements
Rank 1
answered on 23 Oct 2012, 09:00 AM
Hi,
I also want to know if the Kendo dropdown doesn't support to be set by the model like the previous Telerik MVC dropdown?
This should be enough:
@(Html.Kendo().DropDownList()
     .Name("CustomerID")
     .BindTo(Model.Customers)

Without needing to set DataTextField, DataValueField and Value when the Customers is of type IList<SelectListItem>.

Regards,
Mattias
0
Georgi Krustev
Telerik team
answered on 23 Oct 2012, 03:09 PM
Hello Mattias,

 
Next official release of Kendo UI Complete for ASP.NET MVC (scheduled for mid of November) will include this functionality. If ComboBox/DropDownList is bound to List<SelectListItem> will set the DataTextField and DataValueField properties to "Text" and "Value".

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
Mbanaso
Top achievements
Rank 1
answered on 05 Nov 2012, 03:32 AM
Hi
Has this problem been resolved as it didn't work in my scenario. I tried using phil's bit but could not figure out how to.
Any help will be appreciated.
Uche
Tags
DropDownList
Asked by
Paul
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Georgi Krustev
Telerik team
phil
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Mattias
Top achievements
Rank 1
Mbanaso
Top achievements
Rank 1
Share this question
or