从淘汰赛中的下拉列表中访问所选项目值

时间:2014-04-22 17:19:20

标签: knockout.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的{​​{1}}数据吗? 我想将self.CountryId发送到服务器端代码!!

2 个答案:

答案 0 :(得分:0)

您的stateViewModel似乎不完整,但如果您同时拥有州和国家/地区模型,则可以订阅国家/地区中的更改并更新状态:

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

答案 1 :(得分: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);
          });
      }