Json返回" Undefined"到剑道下拉列表?

时间:2016-10-16 09:24:32

标签: jquery json asp.net-mvc kendo-asp.net-mvc kendo-dropdown

您好我正在使用Kendo MVC Grid in cell模式,我正在尝试构建级联下拉列表,我正在尝试根据SubCategory填充Category下拉列表下拉列表。我的一切工作正常,只是我得到JSON返回的结果undefined而不是实际值, 这是代码

@(Html.Kendo().Grid<WebApplication6.Models.SubSubCategory>()
      .Name("grid")
      .Events(events => events.Change("Grid_OnRowSelect"))
      .Columns(columns =>
      {
          columns.ForeignKey(c => c.CategoryID, (System.Collections.IEnumerable)ViewData["Category"],"CategoryID","CategoryName").Title("Category");
columns.ForeignKey(c => c.SubCategoryID (System.Collections.IEnumerable)ViewData["SubCategory"], "SubCategoryID", "SubCategoryName").Title("Sub-Category");

以下是ajax部分: -

<script>
    Grid_OnRowSelect = function (e) {
        var CatID = (this.dataItem(this.select()).CategoryID);
         $.ajax({
                //url: "SubSubCategories/SearchSubCategory",
                url:'@Url.Action("SearchSubCategory", "SubSubCategories")',
                type: "GET",
                data: { CategoryID: CatID },
                dataType: "json",
                success: function (retData) {
                    if (JSON.stringify(retData) != "[]") {
                        var ddl = $('#SubCategoryID').data("kendoDropDownList");
                        ddl.setDataSource(retData);
                        ddl.refresh();



                    }else {
                             alert("No");
                   }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }

            });
<script>

这是简单的Controller(SubSubCategories): -

 public JsonResult SearchSubCategory(int CategoryID)
        {

          var x = ((db.SubCategories.Select(p => 
          new { CategoryID = p.CategoryID, SubCategoryID = p.SubCatgeoryID, SubCategoryName = p.SubCategoryName }))
          .Where(p => p.CategoryID == CategoryID));
            return Json(x, JsonRequestBehavior.AllowGet);
        }

提前致谢:)

1 个答案:

答案 0 :(得分:0)

问题出在JQuery代码中,它从控制器中检索数据,我已将数据放在Array内,然后我绑定了DropDownList Array,这是解决方案: -

    <script>
    Grid_OnRowSelect = function (e) {
        var CatID = (this.dataItem(this.select()).CategoryID);
        //alert(CatID);
        document.getElementById("cat_id").innerHTML = CatID;
            $.ajax({
                url:'@Url.Action("SearchSubCategory", "SubSubCategories")',
                type: "GET",
                data: { CategoryID: CatID },
                success: function (retData) {
                    if (JSON.stringify(retData) != "[]") {
                        //var ddl = $('#SubCategoryID').data("kendoDropDownList");
                        var x = []
                        for (i = 0; i < retData.length; i++) {
                            x.push(retData[i]);
                        }
                        $("#SubCategoryID").kendoDropDownList({
                            dataTextField: "SubCategoryName",
                            dataValueField: "SubCategoryID",
                            dataSource: x
                        });
                    }
                    else {
                        alert("No sub-categories were found for this category");
                    }
                },
                    error: function (jqXHR, textStatus, errorThrown) {
                    //alert(jqXHR.responseText);
                }

            });
    }
</script>