【问题标题】:Kendo Grid Edit Cancel deletes row from Grid剑道网格编辑取消从网格中删除行
【发布时间】:2015-05-07 07:54:04
【问题描述】:

我有一个剑道网格::

   @(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();

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

我已经推荐了this question link ut 这个解决方案对我不起作用。

【问题讨论】:

  • 此解决方案不适合您,因为网格数据绑定在服务器端,而您想在客户端执行插入操作。
  • @JayeshGoyani 即使我将其切换到客户端网格。还是同样的问题。
  • 我会尽力让你知道的。

标签: javascript jquery asp.net-mvc kendo-ui kendo-grid


【解决方案1】:

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

dataSource.add()

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

dataSource.pushCreate(data)

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

我真的希望这对某人有所帮助。我只是从剑道文档某处的一个衬里中发现了这一点。

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

dataSource.pushDestroy(data)

【讨论】:

  • 非常感谢。我花了三个小时寻找解决方案。我怎么会错过 pushCreate 方法???
【解决方案2】:

您必须返回新插入项目的PassengerID。检查ajax editing documentation

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));
}

【讨论】:

    【解决方案3】:

    必须定义模式的 id 属性,它用于内部分析以了解模型是否是新的。

    见: https://www.telerik.com/forums/inline-editing-cancel-removes-the-row

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多