2015-08-18 76 views
0

我一直在摔跤從傳遞模型從視圖到控制器在劍道網格一段時間。我設法讓項目到我的Jsonresult,但所有的值都是'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 
     }); 

回答

0

的問題是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 }; 
         } 
        } 

希望這個作品,如果你有一個問題COMENT就知道了。

+0

Options.models確實有數據,我在chrome上調試過它。 – Porttila