2013-10-17 120 views
0

我正在開發一個應用程序,其中使用產品,術語,位置,出價,報價等字段將數據添加到數據庫表中表格行中的文本框。我正在使用OData並淘汰Js來執行插入和更新。我有以下代碼使用OData將數據推入表格。在Asp.Net MVC中添加一個具有相同名稱但具有不同其他字段的OData的OData

1淘汰賽的js

self.add = function (canadiancrude) { 
    var payload = { 
     Term: "Term", Product: "Product" , Location: "Location", Pipeline: "Pipeline", 
     BidCP: "Bid CP", BidVolume: "Bid Volume", Index: "Index", Bid: "0.0", Offer: "0.0", 
     OfferVolume:"Offer Volume", OfferCP:"Offer CP" 
    }; 
$.ajax({ 
    url: '/odata/Canadiancrudes', 
    type: 'POST', 
    // data: ko.toJSON(payload), 
    data: JSON.stringify(payload), 
    contentType: 'application/json', 
    dataType: 'json' 
});} 

> 2.使用OData的插入

protected override CanadianCrude CreateEntity(CanadianCrude entity) { 
    var newCanadianCrude = db.CanadianCrudes.Add(entity); 
    db.SaveChanges(); return newCanadianCrude;} 

現在我想添加另一條記錄,但具有相同的產品和不同的其餘領域當我單擊添加(圖片中的加號圖像)而不是添加整行時,在父記錄下作爲UI中的層次結構如屏幕截圖所示。

enter image description here

回答

0

我得到了問題的答案我自己

self.copy = function (canadiancrude) { 
     var payload = { 
      Id: canadiancrude.Id, Term: "", Product: canadiancrude.Product, Location: "", Pipeline: "", 
      BidCP: "", BidVolume: "", Index: "", Bid: "0.0", Offer: "0.0", 
      OfferVolume: "", OfferCP: "" 
     }; 
     $.ajax({ 
      url: '/odata/Canadiancrudes', 
      type: 'POST', 
      // data: ko.toJSON(payload), 
      data: JSON.stringify(payload), 
      contentType: 'application/json', 
      dataType: 'json' 
     }); 
    } 
0

只需創建一個函數來做到這一點 -

function copyWhatever(sender) { 
    var whatever = new whateverObject(sender.name); 
    myObservableArray.push(whatever); 
} 

function whateverObject(name) { 
    var self = this; 
    self.Name = ko.observable(name); 
    self.OtherProperties = ko.observable(); 
} 
相關問題