最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Kendo Grid Edit Cancel deletes row from Grid - Stack Overflow

programmeradmin3浏览0评论

I am having a Kendo Grid as::

   @(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(mand => { mand.Edit(); mand.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::

                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.

I am having a Kendo Grid as::

   @(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(mand => { mand.Edit(); mand.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::

                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.

Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked May 7, 2015 at 7:54 RahulRahul 2,3076 gold badges35 silver badges62 bronze badges 3
  • This solution is not worked for you because grid data binding on server side and you want to perform insert operation on client side. – Jayesh Goyani Commented May 7, 2015 at 8:52
  • @JayeshGoyani Even if i am switching it to client Grid. Still the same problem. – Rahul Commented May 7, 2015 at 9:38
  • I will try and let you know. – Jayesh Goyani Commented May 7, 2015 at 12:38
Add a ment  | 

3 Answers 3

Reset to default 8

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.

I really hope this helps someone. I only found this out from a one liner somewhere in the kendo documentation.

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. Check the 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));
}

You MUST define id property of the schema, it is used interanaly to know the model is new or not.

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

发布评论

评论列表(0)

  1. 暂无评论