如何批量更新剑道网格模型

How to batch update Kendo grid model

本文关键字:网格 模型 何批量 更新      更新时间:2023-09-26

在剑道网格中将模型从视图传递到控制器已经有一段时间了。我设法获得项目到我的json结果,但所有的值都是'0'或null。

这是我的代码,任何帮助将不胜感激。

控制器:

public JsonResult ProductsUpdateTicket([DataSourceRequest]DataSourceRequest request, List<TicketProducts> model, int? prodid)
        {
            var entities = new List<TicketProducts>();
            if (model != null && ModelState.IsValid)
            {
                using (var db = new CimDataContext())
                {
                    foreach (var prod in model)
                    {
                    var totalPrice = prod.Quantity * prod.UnitPrice;
                    var entity = new TicketProducts
                    {
                        ID = prod.ID,
                        Quantity = prod.Quantity,
                        TicketNumber = prod.TicketNumber,
                        UnitPrice = prod.UnitPrice,
                        TotalPrice = totalPrice,
                        Description = prod.Description,
                        ProductID = prod.ProductID
                    };
                    entities.Add(entity);
                    db.TicketProducts.Attach(entity);
                    db.Entry(entity).State = System.Data.EntityState.Modified;
                    }
                    db.SaveChanges();
                }
            }
            return Json(new[] { entities }.ToDataSourceResult(request, ModelState));
        }

视图:

var products = new kendo.data.Model.define({
            id: "ID",
            fields: {
                ID: { editable: false, type: "number" },
                ProductID: { type: "number", nullable: false, editable: true },
                Quantity: { type: "number", nullable: false, editable: true },
                Description: { type: "string" },
                UnitPrice: { type: "number" },
                TotalPrice: { type: "number" },
                WorkText: { type: "string" },
                CreatedDateTime: { type: "date" }
            }
        });
var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "/Service/ProductsReadTicket",
                    dataType: "json"
                },
                update: {
                    url: "/Service/ProductsUpdateTicket",
                    dataType: "json",
                    contentType: "application/json"
                },
                create: {
                    url: "/Service/ProductsCreateTicket",
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8'
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return { model: options.models };
                    }
                    if (operation == "read") {
                        return { ticketid: iidee };
                    }
                }
            },
            batch: true,
            pageSize: 20,
            schema: {
                model: products,
                data:
                    function (data) {            
                        return data.Data;
                    }
            }
        });
 $("#grid3").kendoGrid({
            toolbar: ["create", "save", "cancel"],
            dataSource: dataSource,
            sortable: true,
            autobind: false,
            pageable: true,
            selectable: true,
            filterable: true,
            columns: [
                { field: "ID", title: "ID", hidden: true },
                { field: "CreatedDateTime", title: "Pvm", format: "{0:dd.MM.yyyy HH:mm}" },
                { field: "ProductID", title: "Tuotenro" },
                { field: "Description", title: "Nimi" },
                { field: "WorkText", title: "Teksti" },
                { field: "Quantity", title: "Määrä" },
                { field: "UnitPrice", title: "Hinta" },
                { field: "TotalPrice", title: "Kokonaishinta" },
            ],
            editable: true
        });

问题出在options.models。它只是你的模型,本身没有数据。

你可以这样做:

   parameterMap: function (options, operation) {
                        if (operation !== "read" && options.models) {
                            return { model: function() {
                              var grid = $("#grid3").data("kendoGrid");
                              var row = $(grid._editContainer).closest("tr");
                              var rowIdx = $("tr", grid.tbody).index(row);
                             var data = { 
                                          ID : grid._data[rowIdx].ID ,
                                          Quantity :   grid._data[rowIdx].Quantity,
                                          TicketNumber : grid._data[rowIdx].TicketNumber 
                                          UnitPrice  :  grid._data[rowIdx].UnitPrice
                                          TotalPrice :  grid._data[rowIdx].TotalPrice
                                          Description :  grid._data[rowIdx].Description
                                          ProductID :  grid._data[rowIdx].ProductID
                                     };
                             return data;
                             }};
                        }
                        if (operation == "read") {
                            return { ticketid: iidee };
                        }
                    }

希望这能起作用,如果你有问题请留言告诉我