2011-07-11 16 views
32

ドロップダウンリストの初期値を設定する際に問題があります。以下のコードは、ビューモデルの定義と初期化を$(document).readyにしたものです。私は配列の選択値を表すsourceMaterialTypesselectedSourceMaterialTypeという配列を持っています。私は(ASP.Net MVC)モデルとViewBagからの値でビューモデルを初期化しています。ドロップダウン(選択)リストの初期値/デフォルト値のバインド

var viewModel = { 
    sourceMaterialTypes : 
     ko.observableArray(@Html.Raw(Json.Encode(ViewBag.SourceMaterialTypes))), 
    selectedSourceMaterialType : 
     ko.observable(@Html.Raw(Json.Encode(Model.SourceMaterialType))), 
    ingredientTypes : 
     ko.observableArray(@Html.Raw(Json.Encode(ViewBag.IngredientTypes))), 
    selectedIngredientType : ko.observable() 
}; 

$(document).ready(function() { 

    ko.applyBindings(viewModel); 

    viewModel.selectedSourceMaterialType.subscribe(function(newSourceMaterialType) { 
     $.getJSON("/IngredientType/FindByMaterialType", 
        { "id": newSourceMaterialType }) 
      .success(function (data) { 
       viewModel.ingredientTypes($.parseJSON(data)); 
      }) 
      .error(function() { alert("error"); }); 
    }); 
}); 

以下は、ノックアウトバインド定義のドロップダウン(選択)リストの定義です。

<select id="SourceMaterialTypeId" 
     name="SourceMaterialTypeId" 
     data-bind="options: sourceMaterialTypes, 
        optionsText: 'Name', 
        optionsValue : 'Id', 
        value: selectedSourceMaterialType"></select> 

これは、すべてが(selectedSourceMaterialTypeが正しくバインドされているドロップダウン選択は、その値が正しく更新されて変更した場合ので、それは私が問題を抱えています唯一の最初の選択がある原料のドロップダウンリストで最初に選択した値を除いて正常に動作します)、これは常にビューモデルのsourceMaterialTypes配列の最初の項目です。

最初に選択した値は、(サーバー側)モデルからselectedSourceMaterialTypeビューモデルプロパティの値として初期化される値にしたいと考えています。

+0

これは正常に動作するはずです。 > selectedSourceMaterialType:ko.observable(@ Html.Raw(Json.Encode(Model.SourceMaterialType))) '私はそれが空のパラメータだと思っています。 – neebz

+0

@nEEbzいいえ、空でない.... 'selectedSourceMaterialType:ko.observable({" Id ":"名前 ":"フルーツ "、"説明 ":"フレッシュフルーツ "、" MeasuredIn ":1、" MeasuredInValue最初の選択は、sourceMaterialTypesの最初の項目です。sourceMaterialTypes:ko.observableArray([{"Id":1、 "Name": "コーヒー豆 "、"説明 ":"生コーヒー豆 "、" MeasuredIn ":0、" MeasuredInValue ":0}、{" Id ":2、" Name ":" Fruit "、" Description ":" Fresh最初の選択は "Coffee Bean"です) –

+3

私は、selectedSourceMaterialType observable関数内のオブジェクト全体ではなく、IDだけを渡す必要があると思います - > 'selectedSourceMaterialType:ko.observable(2)' – neebz

答えて

12

は、私はあなたがselectedSourceMaterialType観察可能な機能だけではなく、オブジェクト全体IDを渡す必要があると思います - @nEEBzが示唆したように>は

selectedSourceMaterialType: ko.observable(@Model.SourceMaterialType.Id) 
+11

しかし、オブジェクト全体を提供し、optionsValue: 'Id'を使用したくない場合は、どのようにすればよいか知っていますか? – pilavdzice

4

APIにはソリューションがあります。オプションにoptionsCaptionを追加するだけで済みます。

<select id="SourceMaterialTypeId" 
    name="SourceMaterialTypeId" 
    data-bind="options: sourceMaterialTypes, 
       optionsText: 'Name', 
       optionsValue : 'Id', 
       value: selectedSourceMaterialType, 
       optionsCaption: 'Please select...'"></select> 
+10

私は、最初の/デフォルト値をモデル/ビューモデルによって設定されているものにすることに焦点が当てられていると思います。したがって、アイテムを編集するときに、「選択してください...」ではなく、最後に選択した値を表示したい場合は – peteski

+0

ヒント: 'optionsCaption'はオブザーバブル自体です。nullを返した場合、項目はオプションのリストには表示されません。このようにして、 'monthCaption = ko.computed(()=> this.nextShipmentDate_month()?null: ' - 月の選択 - ' ); '(ここにtypescriptがあると仮定します) –

1

selectedSourceMaterialTypeが正しく初期化されます。 learn.knockoutjs.com "Lists and Collections" tutorialでは、オブザーバブル配列の特定のインデックスの値を渡すことによって、ビューモデルのselected-itemプロパティを初期化します。例えば、これを行う:

selectedSourceMaterialType: ko.observable(sourceMaterialTypes[2]) 

を...これに代えて:

selectedSourceMaterialType: ko.observable({"Id":1,"Name":"Coffee Bean" /* ... */}); 

その方法は、選択された項目の値がドロップダウンリスト同じ観察アレイ内の項目への参照でありますアイテムはから来る。

+1

私はそういうことをしているとは思わない?そのため、サーバーから戻って来た結果を余分に処理して、選択する必要のある項目の索引を決定する必要があります。 –

関連する問題