我只为客户端设置了一个Kendo网格。每当我添加一行,然后编辑它,然后取消 - 它将被删除。这里和Kendo论坛已就此问题提出了多个问题,并且所有建议都指向模型ID的错误设置。
嗯,就我而言,ID似乎设置正确。我在onGridSave()
javascript事件中为模型分配了一个新ID,如下所示:
var _curId = 1;
function onGridSave(e) {
var newId = _curId++;
e.model.set('id', newId);
e.model.set('EncryptedIngredientId', newId);
}
当我在添加多行后查看网格中的数据时,它们的所有ID都是唯一的 - 从1到n。
但是当我取消编辑时,在onGridChange()
事件中,action
是“删除”,并且已取消的行被删除。对于新行和已编辑的行都会发生这种情况,而新行应该是这种情况。
网格设置如下:
@(Html.Kendo().Grid<IngredientViewModel>(Model.ServerData)
.Name("IngredientsGrid")
.Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(true))
.BindTo(Model.DataAfterEdit ?? Model.ServerData)
.DataSource(ds => ds
.Ajax()
.ServerOperation(false)
.Events(ev => ev.Change("onGridChange").Error("onGridError"))
.Model(m => {
m.Id(p => p.EncryptedIngredientId);
m.Field(p => p.EncryptedIngredientId).DefaultValue(Guid.NewGuid().ToString());
m.Field(p => p.PercentInfo).DefaultValue(new PercentInfoViewModel());
})
.Read("IngGrid_Read", "Company") // <-- dummy action that doesn't exist in controller
.Update("IngGrid_Update", "Company") // <-- dummy action that doesn't exist in controller
.Create("IngGrid_Create", "Company") // <-- dummy action that doesn't exist in controller
.Destroy("IngGrid_Destroy", "Company")) // <-- dummy action that doesn't exist in controller
.ToolBar(tbar => tbar.Create())
.Columns(c => {
c.AutoGenerate(false);
c.Bound(m => m.CasNumber);
c.Bound(m => m.IngredientName);
c.Bound(m => m.PercentInfo).ClientTemplate("#= makePercentageDisplayString(data.PercentInfo) #").Width(180);
c.Bound(m => m.ReachRegNumber);
c.Bound(m => m.ReachSvhc);
c.Bound(m => m.RohsSubstance);
c.Bound(m => m.Prop65Substance);
c.Command(command => {
command.Edit();
command.Destroy();
}).Width(200);
})
.Events(evt => {
evt.Save("onGridSave");
evt.Edit("onGridEdit");
})
)
我做错了什么?
答案 0 :(得分:0)
当您声明此虚拟操作时,Kendo会在您编辑单元格后尝试保存数据。它执行创建操作但没有来自服务器的正确响应,因此它认为请求失败。因为它在你点击取消后尝试删除行,因为它无法在他们的dataSource中找到它。
如果它只是你所说的本地网格,那么解决方案是在你的网格dataSource配置上添加.Batch(true)
以防止更改单元后的更新操作。
如果您想在服务器上保存已编辑的数据,则应正确实施更新,创建和销毁操作。
答案 1 :(得分:0)
事实证明,使用MVC包装器时不支持此功能,但可以使用JavaScript完成。由于我不想失去C#的类型安全性,我在控制器上实现了CRUD操作并在会话中存储行。
我必须在网格定义中更改的唯一两件事是:设置.ServerOperation(true)
而不是false
;并删除.BindTo(...)
电话。结果代码如下所示:
@(Html.Kendo().Grid<IngredientViewModel>(Model.ServerData)
.Name("IngredientsGrid")
.Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(true))
.DataSource(ds => ds
.Ajax()
.ServerOperation(true)
.Events(ev => ev.Change("onGridChange").Error("onGridError"))
.Model(m => {
m.Id(p => p.EncryptedIngredientId);
m.Field(p => p.EncryptedIngredientId).DefaultValue(Guid.NewGuid().ToString());
m.Field(p => p.PercentInfo).DefaultValue(new PercentInfoViewModel());
})
.Read("IngGrid_Read", "Company") // <-- dummy action that doesn't exist in controller
.Update("IngGrid_Update", "Company") // <-- dummy action that doesn't exist in controller
.Create("IngGrid_Create", "Company") // <-- dummy action that doesn't exist in controller
.Destroy("IngGrid_Destroy", "Company")) // <-- dummy action that doesn't exist in controller
.ToolBar(tbar => tbar.Create())
.Columns(c => {
c.AutoGenerate(false);
c.Bound(m => m.CasNumber);
c.Bound(m => m.IngredientName);
c.Bound(m => m.PercentInfo).ClientTemplate("#= makePercentageDisplayString(data.PercentInfo) #").Width(180);
c.Bound(m => m.ReachRegNumber);
c.Bound(m => m.ReachSvhc);
c.Bound(m => m.RohsSubstance);
c.Bound(m => m.Prop65Substance);
c.Command(command => {
command.Edit();
command.Destroy();
}).Width(200);
})
.Events(evt => {
evt.Save("onGridSave");
evt.Edit("onGridEdit");
})
)