[英]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.