2016-07-05 96 views
1

我閱讀了此處和所有網站上的所有內容,並且不理解我的問題。看起來它應該工作,但它不。我得到[Object object]作爲選項。在knockout.js中選擇下拉列表中的綁定選項和選項文本

的Html

<select data-bind="options: Cities, optionsText: Cities.CityNameRu"></select> 

敲除

function CityModel(data) { 
    this.CityId = ko.observable(data.CityId); 
    this.CityNameRu = ko.observable(data.CityNameRu); 
    this.CityName = ko.observable(data.CityName); 
} 

function IndexModel() { 
    var self = this; 
    self.Cities = ko.observableArray([]); 

    self.GetCities = function() { 
     $.ajax({ 
      type: "GET", 
      url: '/FetchCities', 
      dataType: "json", 
      success: function (data) { 
       self.SuccessfullyRetrievedModelsFromAjax(data); 
      }, 
      error: function (err) { 
       alert(err.status + " : " + err.statusText); 
      } 
     }); 
    }; 

    this.SuccessfullyRetrievedModelsFromAjax = function (models) { 
     ko.utils.arrayForEach(models, function (model) { 
      self.Cities.push(new CityModel(model)); 
     }); 
    }; 
    self.GetCities(); 
} 

JSON響應

[{"CityId":1,"CityName":"philadelphia","CityNameRu":"Филадельфия"},{"CityId":2,"CityName":"new-york","CityNameRu":"Нью Йорк"} 

回答

2

optionsText值是用於文本的每個options數組元素內的屬性的字符串名稱,因此您應該將綁定更改爲:

<select data-bind="options: Cities, optionsText: 'CityNameRu'"></select> 
+0

非常感謝! –

相關問題