Hi,
I have a problem removing a newly added row or clearing updated values during update when calling Ajax Create or Update methods. I've tried e.sender.cancelChanges(); and also the code below with no luck. The error alert shows on the screen but the row sticks in the grid after hitting OK on the error window prompt. Is there a reason why the cancel changes is not updating the Json dataset after the error?
Error JS:
<script type="text/javascript"> function error_handler(e) { if (e.errors) { var message = "Errors:\n"; $.each(e.errors, function (key, value) { if ('errors' in value) { $.each(value.errors, function () { message += this + "\n"; }); } }); //Display the message alert(message); // Cancel the changes. var grid = $("#meterGrid").data("kendoGrid"); grid.cancelChanges(); } }</script>
My grid:
@(Html.Kendo().Grid<MeterSerialsViewModel>() .Name("meterGrid") .Columns(columns => { columns.Command(command => { command.Edit(); }).Width(60).Visible(Model.IsEditable); columns.Bound(o => o.MeterSerialId); columns.Bound(o => o.VendorAccountId).Visible(false); columns.Bound(o => o.MeterSerialName).Width(300); columns.ForeignKey(o => o.CommodityId, (System.Collections.IEnumerable)ViewData["CommodityTypes"], "CommodityTypeId", "Name").EditorTemplateName("GridForeignKeyEditor").Title("Commodity").Width(250); columns.Bound(o => o.IsActive).ClientTemplate("<input type='checkbox' disabled='disabled' #= IsActive ? checked='checked': checked='' # />"); columns.Bound(o => o.CreatedOn).Width(180).Format("{0:MM/dd/yyyy hh:mm tt}"); }) .ToolBar(toolbar => { if (Model.IsEditable) { toolbar.Create(); } }) .Pageable() .Sortable() .Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(Model.IsEditable)) .DataSource(dataSource => dataSource .Ajax() .ServerOperation(false) .PageSize(10) .Events(events => { events.Error("error_handler"); }) .Model(model => { model.Id(o => o.MeterSerialId); model.Field(o => o.MeterSerialId).Editable(false); model.Field(o => o.VendorAccountId).DefaultValue(Model.VendorAccountId).Editable(false); model.Field(o => o.IsActive).DefaultValue(true); model.Field(o => o.CreatedBy).Editable(false); model.Field(o => o.CreatedOn).Editable(false); }) .Sort(s => { s.Add("CommodityTypeName").Ascending(); }) .Read(r => r.Action("GetMeterSerials", "VendorAccounts", new { vendorAccountId = Model.VendorAccountId })) .Create(c => c.Action("CreateMeterSerial", "VendorAccounts", new { vendorAccountId = Model.VendorAccountId })) .Update(u => u.Action("UpdateMeterSerial", "VendorAccounts"))))
Controller to exception testing error prompt:
catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.ToString()); return Json(new[] { model }.ToDataSourceResult(request, ModelState)); }