我的 mvc 项目中有一个剑道网格(内联模式),我通过 mvc 初始化了网格。问题是当我添加一个新行时,它的 id 为 0,并且它的脏属性设置为 true。如何刷新添加的项目并为其设置正确的 ID?
这是我的剑道网格:
@(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"))
)
)
这就是我在服务器端所做的:
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);
}
但是还是不行!