Asp.net mvc Kendo UI MVC内联客户端网格-单击“取消”删除行。不是模型ID问题

Asp.net mvc Kendo UI MVC内联客户端网格-单击“取消”删除行。不是模型ID问题,asp.net-mvc,kendo-ui,kendo-grid,Asp.net Mvc,Kendo Ui,Kendo Grid,我只为客户端设置了剑道网格。每当我添加一行,然后编辑它,然后取消-它会被删除。关于这个问题,在这里和剑道论坛上都提出了很多问题,所有的建议都指向了模型ID的错误设置 嗯,在我的情况下,ID似乎设置正确。我在onGridSave()javascript事件中为模型分配一个新ID,如下所示: var _curId = 1; function onGridSave(e) { var newId = _curId++; e.model.set('id', newId); e.mo

我只为客户端设置了剑道网格。每当我添加一行,然后编辑它,然后取消-它会被删除。关于这个问题,在这里和剑道论坛上都提出了很多问题,所有的建议都指向了模型ID的错误设置

嗯,在我的情况下,ID似乎设置正确。我在
onGridSave()
javascript事件中为模型分配一个新ID,如下所示:

var _curId = 1;
function onGridSave(e) {
    var newId = _curId++;
    e.model.set('id', newId);
    e.model.set('EncryptedIngredientId', newId);
}
@(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");
        })
当我在添加了多行之后查看网格中的数据时,它们的所有ID都是唯一的——从1到n

但是当我取消编辑时,在
onGridChange()
事件中,
操作
为“删除”,取消的行被删除。新行和编辑的行都会发生这种情况,而只有新行才会发生这种情况

网格设置如下所示:

@(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");
            })
)
@(Html.Kendo().Grid(Model.ServerData)
.名称(“IngreditsGrid”)
.Editable(Editable=>Editable.Mode(GridEditMode.InLine).Enabled(true))
.BindTo(Model.DataAfterEdit??Model.ServerData)
.DataSource(ds=>ds
.Ajax()
.ServerOperation(错误)
.Events(ev=>ev.Change(“onGridChange”)。错误(“onGridError”))
.Model(m=>{
m、 Id(p=>p.encryptedGrediEntId);
m、 字段(p=>p.EncryptedingRentid).DefaultValue(Guid.NewGuid().ToString());
m、 字段(p=>p.PercentInfo).DefaultValue(新的PercentInfoViewModel());
})
.Read(“IngGrid_Read”,“Company”)//{
c、 自动生成(假);
c、 界限(m=>m.CasNumber);
c、 绑定(m=>m.IngreditName);
c、 绑定(m=>m.PercentInfo).ClientTemplate(“#=makePercentageDisplayString(data.PercentInfo)#”)。宽度(180);
c、 绑定(m=>m.ReachRegNumber);
c、 绑定(m=>m.ReachSvhc);
c、 绑定(m=>m.rohsubstance);
c、 结合物(m=>m.1);
c、 命令(命令=>{
command.Edit();
command.Destroy();
}).宽度(200);
})
.事件(evt=>{
evt.Save(“onGridSave”);
evt.Edit(“onGridEdit”);
})
)

我做错了什么?

当您声明此虚拟操作时,Kendo试图在您编辑单元格后保存数据。它执行创建操作,但没有来自服务器的正确响应,因此它认为请求失败。因为它试图在您单击“取消”后删除该行,因为它在其数据源中找不到该行

如果只是像您所说的本地网格,那么解决方案是在网格数据源配置上添加
.Batch(true)
,以防止在更改单元格后执行更新操作。


如果要在服务器上保存编辑的数据,应正确执行更新、创建和销毁操作。

我也在这里问了我的问题:

事实证明,这在使用MVC包装器时不受支持,但是可以使用JavaScript来完成。因为我不想失去C#的类型安全性,所以我在控制器上实现了CRUD操作,并在会话中存储行

在我的网格定义中,我只需要更改两件事:设置
.ServerOperation(true)
,而不是
false
;并删除
.BindTo(…)
调用。生成的代码如下所示:

var _curId = 1;
function onGridSave(e) {
    var newId = _curId++;
    e.model.set('id', newId);
    e.model.set('EncryptedIngredientId', newId);
}
@(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");
        })
@(Html.Kendo().Grid(Model.ServerData)
.名称(“IngreditsGrid”)
.Editable(Editable=>Editable.Mode(GridEditMode.InLine).Enabled(true))
.DataSource(ds=>ds
.Ajax()
.ServerOperation(真)
.Events(ev=>ev.Change(“onGridChange”)。错误(“onGridError”))
.Model(m=>{
m、 Id(p=>p.encryptedGrediEntId);
m、 字段(p=>p.EncryptedingRentid).DefaultValue(Guid.NewGuid().ToString());
m、 字段(p=>p.PercentInfo).DefaultValue(新的PercentInfoViewModel());
})
.Read(“IngGrid_Read”,“Company”)//{
c、 自动生成(假);
c、 界限(m=>m.CasNumber);
c、 绑定(m=>m.IngreditName);
c、 绑定(m=>m.PercentInfo).ClientTemplate(“#=makePercentageDisplayString(data.PercentInfo)#”)。宽度(180);
c、 绑定(m=>m.ReachRegNumber);
c、 绑定(m=>m.ReachSvhc);
c、 绑定(m=>m.rohsubstance);
c、 结合物(m=>m.1);
c、 命令(命令=>{
command.Edit();
command.Destroy();
}).宽度(200);
})
.事件(evt=>{
evt.Save(“onGridSave”);
evt.Edit(“onGridEdit”);
})

)

由于行的ID是在
onGridSave()
中本地生成的,因此当前未执行这些操作。添加
.Batch(true)
不会改变任何行为。