2016-04-12 15 views
1

1つのドロップダウン(PrimarySpeciality)の値に基づいて2つのドロップダウン(PrimarySpeciality、PrimarySubSpeciality)を持ち、他のドロップダウン(PrimarySubSpeciality)値を変更する必要があります。knockoutJSのドロップダウンにデフォルト値をバインドできません

ロード時に、 'PrimarySubSpecialities'をロード時にデフォルト値にします。

どうすればいいですか?

私CSHTML:

<div class="nmc-righttab" style="width:265px;"> 
    @Html.DropDownListFor(m => m.User.PSpecialty, Model.PSpecialties, new { id = "ddUserDetails", style = "width:245px;height:25px;", data_bind = "event: {change: primaryChanged}" }, Model.IsReadOnly) 
     </div> 
<div style="width:265px;"> 
    @Html.DropDownListFor(m => m.User.PSubSpecialty,Model.PSubspecialties, new { id = "ddUserDetailsPSubSpeciality", style = "width:245px;height:25px;", data_bind = "options: pSubSpecialities,optionsText: 'Name',optionsValue: 'Id',value:PSubspecialty,enable:isPSpecialitySelected" }) 
    </div> 

私のJSファイル:

this.PSubspecialty = ko.observable($('#ddUserDetails').val()); 
    this.pSubSpecialities = ko.observableArray([]); 
    this.isPSpecialitySelected = ko.observable(false); 

    this.pSpecilaityChanged = function() { 

     var pSpecialityVal = $("#ddUserDetails").val(); 
     if (pSpecialityVal) { 
      model.isPSpecialitySelected(true); 
      pStartIndex = 0; 
      pSubSpecialityUrl = '/User/GetSpec?pSpeciality=' + pSpecialityVal +'&sSpeciality='; 
      loadPSubSpecilaities(); 
     } 
     else 
     { 
      model.isSelected(false); 
     } 
    }; 

が負荷に、私は 'pSubSpeciality' 'として価値の '<>' などのテキストであるためには初期値を設定したいです0 'である。

でも、私はModel.PSubSpecialtiesにアイテムを追加できますが、追加したアイテムをpsubspecialityドロップダウンに表示することができませんでした。

初期値をModel.PSubSpecialitiesのpSubSpecialityDropdownに設定する方法はありますか。

答えて

0

モデル内で、UIを少なくして作業します。 DropDownList項目にchangedイベントを作成する代わりに、subscribevalueの受信変数を変更します。あなたのPrimarySpecialtyドロップダウン、あなたのJSで次に

data_bind = "value: primarySpecialty" 

で:

var self = this;  
this.primarySpecialty = ko.observable(); 
this.isPrimarySpecialtySelected = ko.pureComputed(function() { 
    return !!self.primarySpecialty(); 
}); 
this.primarySubSpeciality = ko.observable(); 
//... 
this.primarySpecialty.subscribe(function (newValue) { 
    if (newValue) { 
     loadPrimarySubSpecialities(); 
     this.primarySubSpeciality('0'); // Set default, by value 
    } 
}); 

PrimarySubSpecialityのドロップダウンがIdメンバーであるオプションを持っている場合は '0'、項目がそれに選択されます。

関連する問題