【问题标题】:Json Returns "Undefined" to Kendo Dropdownlist?Json 将“未定义”返回到 Kendo 下拉列表?
【发布时间】:2016-10-16 09:24:32
【问题描述】:

您好,我正在使用 Kendo MVC Grid in cell 模式,我正在尝试构建级联下拉列表,我正在尝试根据 Category 下拉列表填充 SubCategory 下拉列表。除了我得到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>

这里是简单的控制器(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);
        }

提前致谢:)

【问题讨论】:

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


    【解决方案1】:

    问题出在从控制器检索数据的JQuery 代码中,我已将数据放在Array 中,然后我将DropDownListArray,这里是解决方案:-

        <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2014-11-24
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      相关资源
      最近更新 更多