2014-04-22 50 views
0

通過大量的實驗,我可以綁定我的第一個下拉列表。所以我的下一步是選擇一個下拉列表的值。我遵循下面的方式。訪問從下拉列表中選擇的項目值敲出js

<div id="divcountry">   
      <span>Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&nbsp; 
       <select data-bind="options: CountriesList,optionsText: 'CountryName',optionsValue:'CountryId',value:CountryId,optionsCaption: 'Select Country..'" style="width: 148px"> 
      </select> 
      </div> 

var countryModel = { 

      CountriesList: ko.observableArray([]) 

     }; 


     var countryViewModel = function() { 
      var self = this; 
      self.CountryModel = countryModel; 
      // self.validateCountry = ko.validation.group(self.CountryModel, { deep: true }); 
      self.CountriesList = ko.observableArray([]); 
      self.CountryId = ko.observable(); 

     } 

     var stateModel = { 
      StateId: ko.observable(0), 
      StateName: ko.observable('').extend({ required: true }), 
      ShortName: ko.observable('').extend({ required: true }), 
      IsActive: ko.observable(true), 


      CountryId: ko.observable() 
     }; 

     var stateViewModel = function() { 
      var self = this; 
      self.StateModel = stateModel; 
     // self.validateState = ko.validation.group(self.CountryModel, { deep: true }); 
      self.StatesList = ko.observableArray([]); 
      //Handle Submit 
      self.Submit = function() { 
      // if (self.validateCountry().length == 0) { 
        if (self.StateModel.StateId() > 0) { 
         self.UpdateCountry(); 
        } else { 
         self.AddState(); 
        } 
       // self.Reset(); 
       } 
     // } 

現在我的疑問是什麼,但我們可以訪問countryViewModel數據這是我StateModel self.CountryId? 請給我發送CountryId到服務器端的代碼!

回答

0

你stateViewModel似乎不完整的,但如果你同時擁有州和國家的模式,你可以訂閱該國的變化和更新狀態:

self.StateModel = stateModel; 
self.CountryModel = countryModel; 
self.CountryModel.CountryId.subscribe(function(newValue) { 
    self.StateModel.CountryId(newValue); 
}); 
+0

stateModel不是構造函數??? –

+0

@Aj_sari好的,很好,但是這並沒有改變答案,只是對代碼中最不重要的部分進行了微小的調整。我將編輯代碼。 – xdumaine

0

你可以做這樣的事情來獲得你想要的結果

var countryViewModel = function() { 
      var self = this; 
      self.CountryModel = countryModel; 
      // self.validateCountry = ko.validation.group(self.CountryModel, { deep: true }); 
      self.CountriesList = ko.observableArray([]); 
      self.CountryId = ko.observable().subscribe(function(newValue){ 
      stateModel.CountryId(newValue); 
      }); 
     } 
+0

它會給我下拉列表中選定的項目值? –

+0

其實我不明白你爲什麼要在CountryView模型中添加self.CountryId,因爲在這裏我想存儲stateview模型,其中有一個參數叫做countryid,它在國家視圖模型中存在 –

相關問題