2013-10-23 73 views
0

我試圖使用ajax調用將數據綁定到Kendo下拉列表。調用成功獲取數據,但是kendo下拉菜單不顯示任何數據。將數據加載到Kendo Dropdown

JSON對象

enter image description here

下拉:

<div class="editor-field"> 
       @Html.DropDownListFor(model => model.selectedWorkItem, new SelectList(Model.lstWorkItemType), "Select Below...", new { @style = "width:200px;height:20px" }) 
</div> 

Ajax調用:

<script type="text/javascript"> 
    $(document).ready(function() { 

     $("#selectedProject").change(function() { 

      var valid = $("#selectedProject").val(); 

      if (valid != "Select Below...") { 

       var procemessage = "<option value='0'> Please wait...</option>"; 
       $("#selectedWorkItem").html(procemessage).show(); 
       var murl = "/TFS/TFSProjectDetails/"; 

       $.ajax({ 
        url: murl, 
        data: { pName: valid }, 
        cache: false, 
        type: "POST", 
        success: function (data) { 

         if (JSON.stringify(data) != "[]") { 
          var ddl = $('#selectedWorkItem').data("kendoDropDownList"); 
          for (var x = 0; x < data.length; x++) { 

           ddl.setDataSource(data[x].Text); 
          } 

          ddl.refresh(); 
         } 

        }, 
        error: function (reponse) { 
         alert("error : " + reponse); 
        } 
       }); 
      } 
     }); 

    }); 
</script> 

回答

1

您需要指定值和文本字段劍道下拉首先顯示,

$("#selectedWorkItem").kendoDropDownList({ 
     dataTextField: "Text", 
     dataValueField: "Value", 
    }); 

,然後改變yoour成功方法是這樣來設置的數據源,(I假設data是JSON數組)

success: function (data) {   
     if (JSON.stringify(data) != "[]") { 
       var ddl = $('#selectedWorkItem').data("kendoDropDownList"); 
       ddl.setDataSource(data); 
     } 
},