繁体   English   中英

Kendo Grid Edit取消从Grid中删除行

[英]Kendo Grid Edit Cancel deletes row from Grid

我有一个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"))
                        )
                    )

我在其中使用Javascript手动添加新行::

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

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

但问题是当我尝试编辑并在编辑时按下取消按钮,然后该行将从网格中删除。

我已经提到了这个问题链接这个解决方案对我不起作用。

问题是当您使用添加到数据源时

dataSource.add()

它内部在项目上放置一个“新”标志。 因此,如果您取消该项目,它将被删除。 我不知道为什么他们这样做,这是有史以来最愚蠢的事情。 要使用取消按钮进行内联编辑,您需要动态添加自己的数据

dataSource.pushCreate(data)

这实际上做了同样的事情。 但是,它还允许您检查_pristineData中更新的旧数据。

我真的希望这有助于某人。 我只在kendo文档中的某个地方找到了这个。

删除也是如此。 执行此操作的数据源函数是

dataSource.pushDestroy(data)

您必须返回新插入项的PassengerID。 检查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));
}

必须定义模式的id属性,它被用于了解模型是否是新的。

请参阅: 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