Reputation: 435
My grid has all crud operations working, the only problem is that after I save a new record, the grid does not update to the new ID from the controller. I have verified that the ID is being sent back from the controller with the model, but the grid does not update. Therefore any subsequent changes to the same record without a page refresh create a new record.
Controller:
[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");
})
)
How do I make the grid update the new ID without calling read and going back to the database?
Upvotes: 4
Views: 2071
Reputation: 435
After looking through several similar posts and finding a Telerik post that explained that the schema is predefined when using razor and expects data that looks like:
{
"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
}
I was able to modify my controller to return the Json in the way that was expected and the grid does update the ID. Here's the controller code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddHoleToJob([DataSourceRequest] DataSourceRequest request, PlannedHolesVM viewModel)
{
_jobInfoService.AddHole(viewModel);
return Json(new { Data = viewModel });
}
Upvotes: 4
Reputation: 340
Check the database, to see if ID is set as auto incerment , remove Auto increment from id field
Upvotes: -2