2012-03-23 13 views
1

オブジェクトを選択リスト値として使用しているときに、ノックアウト選択リストバインドに失敗しています。文字列を使用するとうまく動作しますが、オブジェクトをバインドする必要があります。Knockout.jsオブジェクトへの値バインディングの選択

私にはギフトオブジェクトがあり、タイトル、価格、会社があります。私は企業の選択リストを持っており、各企業はIDと名前を持っています。ただし、最初の選択は選択リストでは正しくありません。

フィドルを参照してください。http://jsfiddle.net/mrfunnel/SaepM/

MVC3ビューモデルにバインドするときにこれが私にとって重要です。私は物事を間違ったやり方でやっているからかもしれないと私は認めますが。

私は次のモデルがある場合:

public class Company 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 

} 
public class GiftModel 
{ 
    public Company Company { get; set; } 
    public string Title { get; set; } 
    public double Price { get; set; } 
} 

を、私は私のコントローラに結合可能である会社をどのように選択するのですか? CompanyIdプロパティをGiftModelに追加してそれにバインドするか、カスタムバインダーを作成する必要がありますか。ここに根本的な何かが欠けていますか?

ありがとうございます。

答えて

3

多くのことをする必要があります。

ViewModelのCompanyIdは、これをバインドしてこれを観測可能にする唯一の方法です。 あなたが作ることができない観測可能なオブジェクトは、it'sは

<form class="giftListEditor" data-bind="submit: save"> 
    <!-- Our other UI elements, including the table and ‘add’ button, go here --> 

    <p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p> 
    <table> 
     <tbody data-bind="foreach: gifts"> 
      <tr> 
       <td>Gift name: <input data-bind="value: Title"/></td> 
       <td>Price: $ <input data-bind="value: Price"/></td> 
       <td>Company: <select data-bind="options: $root.companies, optionsText: 'Name', optionsValue: 'Id', value: CompanyId"/></td> 
       <td>CompanyId: <span data-bind="text: CompanyId"></span></td> 
       <td><a href="#" data-bind="click: $root.removeGift">Delete</a></td> 
      </tr> 
     </tbody> 
    </table> 
    <button data-bind="click: addGift">Add Gift</button> 
    <button data-bind="enable: gifts().length > 0" type="submit">Submit</button> 
</form>​ 

モデル

// Fake data 
var initialData = [ 
    { Title: ko.observable('Item 1'), Price: ko.observable(56), CompanyId: ko.observable(1) }, 
    { Title: ko.observable('Item 2'), Price: ko.observable(60), CompanyId: ko.observable(2) }, 
    { Title: ko.observable('Item 3'), Price: ko.observable(70), CompanyId: ko.observable(2) } 
]; 

var initialCompanies = [ 
    { Id: 1, Name: "Comp 1" }, 
    { Id: 2, Name: "Comp 2" }, 
    { Id: 3, Name: "Comp 3" } 
]; 

var viewModel = { 
    gifts: ko.observableArray(initialData), 
    companies: initialCompanies, 

    addGift: function() { 
     this.gifts.push({ 
      Title: "", 
      Price: "", 
      Company: { Id: "", Name: "" } 
     }); 
    }, 
    removeGift: function($gift) { 
     viewModel.gifts.remove($gift); 
    }, 
    save: function() { 
     console.log(ko.toJS(viewModel.gifts)); 
    } 
}; 

ko.applyBindings(viewModel, document.body);​ 
+0

この度はお返事ありがとうございます。私はこれが問題を解決する唯一の方法だと思っています。あなたはチャンピオンです。 – TheGwa

0

が結合foeachを使用し、オブジェクトが観測可能にするために値。あなたはこのようなシナリオがある場合:

<span><a href="#" data-bind="text: myObj.label"></a></span> 

をしかし、foreachのバインディングを使用:

<span data-bind="foreach: myObj"><a href="#" data-bind="text: label"></a></span> 

var model = { 
    myObj : ko.observable(); 
} 

をあなたはそれが動作しませんmyObj.labelに結合しようとした場合koは通常のjavascriptの方法で配列を通るようにプロパティを反復処理します。

関連する問題