2013-11-15 86 views
0

我正在使用基因敲除js,ASP.Net MVC應用程序,我有一個下拉列表與一些數據綁定使用基因敲除Js。現在我想將選定的值添加到數據庫中。從下拉列表中存儲值使用敲除js

我這樣做以下方式

HTML

<select> 
       <!--ko foreach: products--> 
       <option data-bind="text: $data, value: newproductnametoadd"></option>  
       <!--/ko-->      
    </select> 
<a class="btn" data-bind="click: add">Add New Entry</a> 

淘汰賽JS

self.products = ko.observableArray(); 
    self.datainput = ko.observableArray(); 

self.newproductnametoadd = ko.observable('Product'); 
    self.add = function (canadiancrude) { 
     var payload = { 
       Product: self.newproductnametoadd(), 
Location: "Location", Pipeline: "Pipeline" 
     }; 
     $.ajax({ 
      url: '/odata/Canadiancrudes', 
      type: 'POST', 
      // data: ko.toJSON(payload), 
      data: JSON.stringify(payload), 
      contentType: 'application/json', 
      dataType: 'json' 
     }); 
    } 
$.ajax({ 
     dataType: "json", 
     url: '/odata/Canadiancrudes', 
     data: ko.toJSON(self.products), 
     async: false, 
     success: function (data) { 
      self.datainput((ko.utils.arrayMap(data.value, function (canadiancrude) { 
       var obsCanadianCrude = { 
        Id: canadiancrude.Id, 

        Product: canadiancrude.Product, 
Location: ko.observable(canadiancrude.Location), 
        Pipeline: ko.observable(canadiancrude.Pipeline) 

       } 
       self.watchModel(obsCanadianCrude, self.modelChanged); 
       return obsCanadianCrude; 
      }))); 
     } 
    }); 

    self.subsetcanadiancrudes = ko.observableArray(self.datainput()).extendsdistinct('Product'); 
    self.products = ko.computed(function() { 
     var products = ko.utils.arrayMap(self.subsetcanadiancrudes(), function (item) { 
      return item.Product; 
     }) 
     return ko.utils.arrayGetDistinctValues(products).sort(); 
    }); 

從數據庫成功填充數據到下拉列表,但是當我點擊添加新條目,但不會將其添加到數據庫中。

回答

0

得到了解決如下

HTML

<select data-bind="options:$root.canadiancrudes, optionsText:'Product', optionsValue:'Product',value:selectedChoice, optionsCaption:'Choose a Product'"> 

</select> 
<input type="button" class="btn btn-success" data-bind="click: add, enable: selectedChoice" value="Add New Entry"/> 

敲除JS

self.selectedChoice = ko.observable(); 
    self.add = function (canadiancrude) { 
     var payload = { 
      Term: "Term", Product: this.selectedChoice(), 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' 
     }); 
    } 
相關問題