I have a kendo grid (inline mode) in my mvc project and I initialized the grid all by mvc.
The problem is when I add a new row it gets a 0 id and it’s dirty property is set to true.
How can I refresh the added item and set the correct id to it?
This is my Kendo Grid:
@(Html.Kendo().Grid<IranHost.Tools.Services.Core.DataModel.Site>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Domain).Width(250).Title("دامین");
columns.Command(command => { command.Edit().Text("ویرایش").UpdateText("ذخیره").CancelText("لغو"); command.Destroy().Text("حذف"); });
})
.ToolBar(toolbar => toolbar.Create().Text("افزودن دامین جدید").HtmlAttributes(new { @class = "add-button" }))
.Editable(editable => { editable.Mode(GridEditMode.InLine); })
.Sortable()
.Pageable()
.Scrollable()
.Events(action => { action.Edit("gridEdit"); action.Save("gridSave"); action.SaveChanges("gridSaveChanges"); })
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => { events.Error("result_handler"); })
.Model(model => model.Id(p => p.Id))
.Create(create => create.Action("AddDomain", "Service", new { customerID = ViewBag.CustomerId }))
.Read(read => read.Action("GetDomainListForGrid", "Service", new { customerID = ViewBag.CustomerId }))
.Update(update => update.Action("EditDomain", "Service"))
.Destroy(destroy => destroy.Action("DeleteDomain", "Service"))
)
)
And This is what I’ve done in the server side:
public ActionResult AddDomain([DataSourceRequest] DataSourceRequest request, DataModel.Site site)
{
if (ModelState.IsValid)
{
var pattern = "([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w\\.-]*)";
if (!Regex.IsMatch(site.Domain, pattern))
{
//TODO: Must be added in the framework.
ModelState.AddModelError("ERROR", "Wrong URL Format!");
return Json(ModelState.ToDataSourceResult(), JsonRequestBehavior.AllowGet);
}
var siteContext = new Biz.Site(DataContext);
siteContext.Add(site);
DataContext.SaveChanges();
ModelState.AddModelError("ADDED", site.id);
return Json(new[] { site }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
}
return new HttpStatusCodeResult(403);
}
But still it doesn’t work!
Problem Solved!
I was using older version of KendoUI and it didn’t have requestEnd so I started using AddModelError to handle server side results.
Actually I used AddModelError not for error but just telling the client side that adding/editing the item was with success.
It was actually a very bad idea but I didn’t have a choice at moment. In the client side I was raising Events.Error(“result_handler”). Kendo though that it really is an error so it didn’t made any changes on the ui but setting the dirty property to true!
Now my boss is trying to get new version of KendoUI in the meanwile I’m using ajaxComplete to raise other events. Unfortunately I don’t really have much of a choice until I get the newer version!