问题描述
我有一个标准的C#ASP.NET MVC控制器的以下code。
I have the following code in a standard C# ASP.NET MVC controller.
public JsonResult ReadTeachers()
{
return Json(ReadTeacherData(), JsonRequestBehavior.AllowGet);
}
public void UpdateTeachers(IEnumerable<Teacher> updatedTeachers)
{
// this is never called
}
我试图调用这个控制器与KendoGrid。这里是code为我的网格。
I'm trying to call this controller with a KendoGrid. Here is the code for my grid.
$("#teachers").kendoGrid({
dataSource: {
type: "json",
transport: {
read: {
url: '@Url.Action("ReadTeachers", "EducationPortal")',
dataType: "json"
},
update: {
url: '@Url.Action("UpdateTeachers", "EducationPortal")',
dataType: "json"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return { models: kendo.stringify(options.models) };
}
}
},
batch: true,
schema: {
model: {
id: "TeacherId",
fields: {
TeacherId: { type: "number" },
FullName: { type: "string" },
IsHeadmaster: { type: "boolean" }
}
}
}
},
toolbar: ["create", "save"],
columns: [
{ field: "FullName", title: "Teacher" },
{ field: "IsHeadmaster", title: "Is a Headmaster?", width: "120px" },
{ command: ["destroy"], title: " ", width: "85px" }],
editable: true
});
我适应这个code从剑道的 。问题是,该UpdateTeachers方法不会被调用。我怀疑问题出在parameterMap的功能,因为这是code我了解最少的部分。
I adapted this code from Kendo's examples. The problem is, the UpdateTeachers method is never called. I suspect that the issue lies in the parameterMap function, because that's the part of the code I understand the least.
推荐答案
而不是使用
public void UpdateTeachers(IEnumerable<Teacher> updatedTeachers)
{
// this is never called
}
用
public JsonResult UpdateTeachers(string models)
{
//Deserialize to object
IList<Teacher> teachers= new JavaScriptSerializer().Deserialize<IList<Teacher>>(models);
return Json(Teacher)
}
需要注意的是parameterMap的:()函数发送更新的数据序列化字符串格式名称为模式,所以你应该在你的动作用模型作为参数名
Note that parameterMap: function() send updated data in serialize string format with name models so you should use "models" as parameter name in your action
我希望这会帮助你。
这篇关于为什么我的KendoGrid叫我的MVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!