简体   繁体   English

Kendo Grid Edit取消从Grid中删除行

[英]Kendo Grid Edit Cancel deletes row from Grid

I am having a Kendo Grid as:: 我有一个Kendo Grid ::

   @(Html.Kendo().Grid<Models.PassengerGrid>()
                        .Name("Passenger")
                        .Columns(columns =>
                        {
                            columns.Bound(x => x.PassengerID).Hidden(true);
                            columns.Bound(x => x.Name).Title("Name").Width(500).Encoded(true);
                            columns.Command(command => { command.Edit(); command.Destroy(); });
                        })
                        .Editable(editable => editable.Mode(GridEditMode.InLine))
                        .HtmlAttributes(new { style = "height:430px;" })
                        .DataSource(dataSource => dataSource
                            .Ajax()
                            .PageSize(5)
                            .ServerOperation(true)
                            .Model(model => { model.Id(p => p.PassengerID); })
                            .Read(read => read.Action("PassengerDetailTemplate", "GetData"))
                            .Create(update => update.Action("EditingPopup_Update", "Grid"))
                            .Update(update => update.Action("EditingPopup_Update", "Grid"))
                            .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
                        )
                    )

In which I am adding new Row manually by using Javascript as:: 我在其中使用Javascript手动添加新行::

                var Grid = $("#Passenger").data("kendoGrid");
                            var datasource = Grid.dataSource;

                            datasource.add({
                                PassengerID: response.PassengerID,
                                Name: response.Name
                            });
                            datasource.sync();

But problem is when I am trying to edit & press cancel button while editing, Then the row gets deleted from Grid. 但问题是当我尝试编辑并在编辑时按下取消按钮,然后该行将从网格中删除。

I have referred this question link ut this solution is not working for me. 我已经提到了这个问题链接这个解决方案对我不起作用。

The issue is that when you add to the datasource using 问题是当您使用添加到数据源时

dataSource.add()

It internally puts a "new" flag on the item. 它内部在项目上放置一个“新”标志。 So if you cancel the item it gets removed. 因此,如果您取消该项目,它将被删除。 I have no idea why they do this, it's the dumbest thing ever. 我不知道为什么他们这样做,这是有史以来最愚蠢的事情。 To make inline editing work with the cancel button and you add your own data on the fly you need to call 要使用取消按钮进行内联编辑,您需要动态添加自己的数据

dataSource.pushCreate(data)

This effectively does the same thing. 这实际上做了同样的事情。 However, it also allows you to check the old data on an update in _pristineData. 但是,它还允许您检查_pristineData中更新的旧数据。

I really hope this helps someone. 我真的希望这有助于某人。 I only found this out from a one liner somewhere in the kendo documentation. 我只在kendo文档中的某个地方找到了这个。

This is also the case with remove. 删除也是如此。 The datasource function that does that is 执行此操作的数据源函数是

dataSource.pushDestroy(data)

You have to return the PassengerID of the newly inserted item. 您必须返回新插入项的PassengerID。 Check the ajax editing documentation : 检查ajax编辑文档

public ActionResult Products_Create([DataSourceRequest]DataSourceRequest request, ProductViewModel product)
{
    if (ModelState.IsValid)
    {
        using (var northwind = new NorthwindEntities())
        {
            // Create a new Product entity and set its properties from the posted ProductViewModel
            var entity = new Product
            {
                ProductName = product.ProductName,
                UnitsInStock = product.UnitsInStock
            };
            // Add the entity
            northwind.Products.Add(entity);
            // Insert the entity in the database
            northwind.SaveChanges();
            // Get the ProductID generated by the database
            product.ProductID = entity.ProductID;
        }
    }
    // Return the inserted product. The grid needs the generated ProductID. Also return any validation errors.
    return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}

You MUST define id property of the schema, it is used interanaly to know the model is new or not. 必须定义模式的id属性,它被用于了解模型是否是新的。

see: https://www.telerik.com/forums/inline-editing-cancel-removes-the-row 请参阅: https//www.telerik.com/forums/inline-editing-cancel-removes-the-row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM