DropDownList seems to lose its data source after I change the Index, or Disable it.

2 posts, 0 answers
  1. Pawel
    Pawel avatar
    1 posts
    Member since:
    Oct 2015

    Posted 26 May Link to this post

    I have a C# / ASP.NET web application using Kendo web UI controls and Razor Views. Depending on the user's selection of one drop down ("ddlModelProperty"), it needs to enable or disable other  dropdowns ("ddlSomeOtherModelProperty").

     

    In my View file, I have a DropDownList defined like so:

    @(Html.Kendo().DropDownListFor(m => m.ModelProperty).HtmlAttributes(new { id = "ddlModelProperty", @class = "k-dropdown-width-30", @tabIndex = "1", style = "width:60px", onchange = "OnChangeModelProperty(this);" }).BindTo(ViewBag.ZeroToOne).OptionLabel(" "))

     

    @(Html.Kendo().DropDownListFor(m => m.SomeOtherModelProperty).HtmlAttributes(new { id = "ddlSomeOtherModelProperty", @class = "k-dropdown-width-30", @tabIndex = "1", style = "width:60px" }).BindTo(ViewBag.ZeroToThree).OptionLabel(" "))

     

    In the controller, I have a method that returns the ViewResult. In this method, the ViewBag is populated like so:

    ViewBag.ZeroToOne = FillDropDownValues(dtData, "ZeroToOne", "Notes", "Item");

    ViewBag.ZeroToThree = FillDropDownValues(dtData, "ZeroToThree", "Notes", "Item");

    The FillDropDownValues method returns a List<System.Web.Mvc.SelectListItem> object containing the values to be displayed in the drop down.

     

    The OnChangeModelProperty(this) method referenced in the onchange HtmlAttribute for ddlModelProperty performs the following:

    var cmbVal = $("#" + cmb.id).val();
     var enabled = (cmbVal != "0");
     
     if (!enabled)
        $("#ddlSomeOtherModelProperty").kendoDropDownList({ index: 0 });
     
    $("#ddlSomeOtherModelProperty").kendoDropDownList({ enable: enabled});

     

    The problem is this:

     

    My page loads, all drop downs populate and are able to be interacted with as expected. If I select "0" for ddlModelProperty, it disables ddlSomeOtherModelProperty as I expect it to. Then I change ddlModelProperty to a different value. This causes ddlSomeOtherModelProperty to be re-enabled, however when I click the Arrow to expand the DropDown menu nothing appears.

     

    This leads me to believe 1 of 2 things is happening:

    Either the DataSource is somehow being wiped out and the animation plays-- it just has nothing to display, or simply the animation is broken and isn't displaying at all.

     

    What am I doing incorrectly?

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 30 May Link to this post

    Hello,

    The data will be lost because with the used code:
    $("#ddlSomeOtherModelProperty").kendoDropDownList({...});
    you are initializing a new dropdownlist instance. You should get the existing instance and use the select and enable methods to change the selected index and enabled state e.g.
    $("#ddlSomeOtherModelProperty").data("kendoDropDownList").enable(false);


    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
Back to Top