我的网格所有crud操作都有效,唯一的问题是我保存新记录后,网格不会从控制器更新为新ID。我已经验证了ID是从模型控制器发回的,但是网格没有更新。因此,在没有页面刷新的情况下对同一记录的任何后续更改都会创建新记录。
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddHoleToJob([DataSourceRequest] DataSourceRequest request, PlannedHolesVM viewModel)
{
_jobInfoService.AddHole(viewModel);
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
CSHTML:
@(Html.Kendo().Grid<PlannedHolesVM>()
.Name("PlannedHolesKendoGrid")
.Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("HoleDetails"))
.Columns(c =>
{
c.Bound(m => m.ID);
c.Bound(m => m.HoleSectionLookupName);
c.Bound(m => m.HoleSectionTypeLookupHoleSectionType);
c.Bound(m => m.HoleSize);
c.Command(command =>
{
command.Edit();
command.Destroy();
});
})
.ClientDetailTemplateId("template")
.ToolBar(toolbar => toolbar.Create().Text("Add Hole"))
.DataSource(ds => ds
.Ajax()
.Model(model =>
{
model.Id(c => c.ID);
model.Field(c => c.JobID);
model.Field(c => c.JobID).DefaultValue(ViewBag.Jobid);
model.Field(c => c.HoleSectionID);
model.Field(c => c.HoleSectionTypeID);
})
.Read(read => read.Action("PlannedHolesGridData", "JobRecord", new { jobid = ViewBag.Jobid }))
.Destroy(delete => delete.Action("DeleteHoleFromJob", "JobRecord"))
.Create(create => create.Action("AddHoleToJob", "JobRecord"))
.Update(update => update.Action("AddHoleToJob", "JobRecord"))
)
.Events(e =>
{
e.DataBound("dataBound");
e.Save("refreshOnSave");
})
)
如何在不调用read并返回数据库的情况下使网格更新新ID?
答案 0 :(得分:2)
查看了几篇类似的帖子并找到了一篇Telerik帖子,该帖子解释了使用razor时预定义了架构,并期望看起来像这样的数据:
{
"Data": [{
"ProductID": 1,
"ProductName": "Chai8",
"UnitPrice": 18.00,
"UnitsInStock": 39,
"Discontinued": false
}, {
"ProductID": 2,
"ProductName": "Chang",
"UnitPrice": 19.00,
"UnitsInStock": 17,
"Discontinued": false
}],
"Total": 2,
"AggregateResults": null,
"Errors": null
}
我能够修改我的控制器,以预期的方式返回Json,并且网格确实更新了ID。这是控制器代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddHoleToJob([DataSourceRequest] DataSourceRequest request, PlannedHolesVM viewModel)
{
_jobInfoService.AddHole(viewModel);
return Json(new { Data = viewModel });
}
注意首都'D'。
答案 1 :(得分:0)
检查数据库,查看 ID 是否设置为自动输入,从ID字段中删除自动增量