Specify Selected Item when get data remotely

2 posts, 1 answers
  1. John
    John avatar
    23 posts
    Member since:
    Jan 2015

    Posted 20 Feb 2015 Link to this post

    Using the ASP.NET MVC Extensions, I am loading a DropDownList with data from the server.
    How can I also pass down from the server which of the items in the DDL I would like to be selected? I always need to have at least one item selected and it will most likely not be the first item in the list. Until I fetch the data for the current user, I will not know which item should be selected.

    This is my current code:

          .HtmlAttributes(new { style = "width:300px" })
          .DataSource(source =>
                  source.Read(read => { read.Action("GetCompanies", "CommonAjax"); })

  2. Answer
    Georgi Krustev
    Georgi Krustev avatar
    3687 posts

    Posted 24 Feb 2015 Link to this post

    Hello John,

    In general, the widget has Value method that allows to set the selected value:
           .Value("selected value")

    If you would like to get the selected value from the server during the Ajax request, then I can think of two possible ways to accomplish your goal:
    • Make a second request in order to get the widget's value from server then set the value using its value method
    • The company object should have a field that marks it as selected.​ With this information, you can wire the DataBound event of the data source, loop though the data items and once the selected item is found to select the value of the widget accordingly:
      //Widget initialization is here
        $(function() {
          var dropdownlist = $("#companies").data("kendoDropDownList");
          //bind event only once
          dropdownlist.one("dataBound", function() {
            var data = this.dataSource.view();
            for (var idx = 0; idx < data.length; idx++) {
               if (data[idx] is selected) {

    Georgi Krustev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top