问题
I have the following code in my Index.cshtml
file:
var dataSource = new kendo.data.DataSource({
type: "json",
transport: {
read: {
url: '@Url.Action("ReadTeachers", "EducationPortal")',
dataType: "json"
},
update: {
url: '@Url.Action("UpdateTeachers", "EducationPortal")',
type: "POST"
},
parameterMap: function (data, operation) {
if (operation != "read"){
var result = {};
for (var i = 0; i < data.models.length; i++) {
var teacher = data.models[i];
for (var member in teacher) {
result["teacher[" + i + "]." + member] = teacher[member];
}
}
return result;
} else {
return JSON.stringify(data);
}
}
},
batch: true,
schema: {
model: {
id: "TeacherId",
fields: {
TeacherId: { type: "number" },
FullName: { type: "string" },
IsHeadmaster: { type: "boolean" }
}
}
}
});
$("#teachers").kendoGrid({
dataSource: dataSource,
toolbar: ["create", "save"],
columns: [
{ field: "FullName", title: "Teacher" },
{ field: "IsHeadmaster", title: "Is a Headmaster?", width: "120px" },
{ command: ["destroy"], title: " ", width: "85px" }],
editable: true
});
This is a standard KendoGrid with "batch" editing. The editing and the grid itself both work fine, but the backend doesn't. When the "update" request goes through, it goes to this controller method:
public void UpdateTeachers(string models)
{
// this method will have real code later
Console.Write(models);
}
When I put a breakpoint in here, Visual Studio shows models
to be null
. Like so:
Why is it null
?
回答1:
In your parameterMap: function ()
instead of using
return JSON.stringify(data);
use
return { models: kendo.stringify(data) };
here your data is serialize and attached to a name models now this name models is used as parameter name in your action
回答2:
You need to name the parameter in your controller inside of your parameter map function. Also it looks like you are trying to send the entire data object instead of the model. I would try this within your parameter map function:
parameterMap: function (data, operation) {
if (operation === "update" && data.models){
return {models: kendo.stringify(data.models) };
}
}
回答3:
- Use .Batch(true) method inside .DataSource() in view of concerned action method
Use below code inside kendo grid to create toolbar
.ToolBar(toolbar => { toolbar.Create(); toolbar.Save(); })
Edit mode of grid must be incell to add add more than one entry or in batch mode like below sample code
.Editable(ed => ed.Mode(GridEditMode.InCell).TemplateName("EditDocket").Window(w => w.Title("Docket").Name("editWindow").Width(377).Height(200).Scrollable(true)))
Use below sample code to fetch model details binds to view in controller's action method
[HttpPost] public ActionResult AppEditDocketCreate([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<UDocket> uDockets) { try { if (uDockets != null && ModelState.IsValid) { using (IVSourceEntities db = new IVSourceEntities()) { TBL_Docket tblDocket = new TBL_Docket(); foreach (var uDocket in uDockets) { tblDocket.DocketName = uDocket.DocketName; tblDocket.CreatedDate = System.DateTime.Now; db.TBL_Docket.Add(tblDocket); db.SaveChanges(); } } } } catch (DbEntityValidationException e) { } return Json(ModelState.ToDataSourceResult()); }
Only thing that needed is [Bind(Prefix = "models")]IEnumerable uDockets) as parameter to concerned action method
来源:https://stackoverflow.com/questions/22622061/why-are-my-kendogrid-update-parameters-always-null-in-the-controller