使用OData和Knockout Js将文本框中的文本添加到数据库表中

时间:2014-02-18 17:53:51

标签: javascript sql html5 knockout.js odata

我试图在一些下拉菜单和文本框中添加一个值到sql数据库中的表。它与下拉列表工作正常但是使用文本框它会向表列添加NULL值,即使我已在文本框中输入值

HTML

<td>

                <select data-bind="options: $root.locationNames, optionsText: 'LocationName', optionsValue: 'LocationName', value: selectedLocation, optionsCaption: 'Location'">
                </select>
            </td>

            <td>

                <select data-bind="options: $root.pipelineNames, optionsText: 'PipelineName', optionsValue: 'PipelineName', value: selectedPipeline, optionsCaption: 'Pipeline'">
                </select>
            </td>


            <td>

                <select data-bind="options: $root.counterpartyNames, optionsText: 'CounterPartyName', optionsValue: 'CounterPartyName', value: selectedBidCounterParty, optionsCaption: 'Bid CP'">
                </select>
            </td>

<td>
                <input type="text" name="bidVolume" data-bind="value: $root.BidVolume"/>
            </td>

            <td>
                <input type="text" name="bid" data-bind="value: $root.Bid"/>
            </td>

            <td>
                <input type="text" name="offer" data-bind="value: $root.Offer"/>
            </td>

            <td>
                <input type="text" name="offerVolume" data-bind="value: $root.OfferVolume"/>
            </td>
<td >
                <input type="button" class="btn btn-success" data-bind="click: add, enable: selectedChoice" value="Add New Entry" />
            </td>

使用OData淘汰Js

self.selectedChoice = ko.observable();
    self.selectedTerm = ko.observable();
    self.selectedLocation = ko.observable();
    self.selectedPipeline = ko.observable();
    self.selectedIndex = ko.observable();
    self.selectedBidCounterParty = ko.observable();
    self.selectedOfferCounterParty = ko.observable();

    self.bidVolume = ko.observable();
    self.bid = ko.observable();
    self.offer = ko.observable();
    self.offerVolume = ko.observable();



    self.add = function (canadiancrude) {
        var payload = {
            Term: this.selectedTerm(), Product: this.selectedChoice(), Location: this.selectedLocation(), Pipeline: this.selectedPipeline(),
            BidCP: this.selectedBidCounterParty(), BidVolume: this.bidVolume(), Index: this.selectedIndex(), Bid: this.bid(), Offer: this.offer(),
            OfferVolume: this.offerVolume(), OfferCP: this.selectedOfferCounterParty()//, Locked: "0", Sequence: "", TermID: "0"
        };
        $.ajax({
            url: '/odata/Canadiancrudes',
            type: 'POST',
            //  data: ko.toJSON(payload),
            data: JSON.stringify(payload),
            contentType: 'application/json',
            dataType: 'json'
        });
    }

我可以知道我在哪里弄错了吗?

1 个答案:

答案 0 :(得分:0)

修正了绑定数据的小错误,如下所示

                             

        <td>
            <input type="text"  data-bind="value: bid"/>
        </td>

        <td>
            <input type="text"  data-bind="value: offer"/>
        </td>

        <td>
            <input type="text" data-bind="value: offerVolume"/>
        </td>