您好我正在使用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);
}
提前致谢:)
答案 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>