Javascript 如何在ObservalArray中绑定json字符串?

Javascript 如何在ObservalArray中绑定json字符串?,javascript,jquery,html,json,knockout.js,Javascript,Jquery,Html,Json,Knockout.js,在我的控制器中: public ActionResult GetCountryList() { return Json(new {data = db.Country.ToList()},JsonRequestBehavior.AllowGet); } 在我的发言中: self.GetCountryList = function () { $.getJSON("/ProfileCore/GetCountryList", function (result) {

在我的控制器中:

 public ActionResult GetCountryList() {
     return Json(new {data = db.Country.ToList()},JsonRequestBehavior.AllowGet);
 }
在我的发言中:

self.GetCountryList = function () {
    $.getJSON("/ProfileCore/GetCountryList", function (result) {
        self.LocationList(result.data);
        console.log(self.LocationList());
    })
};
选择html:

<select data-placeholder="Location" class="chosen-select" style="width:100%;" tabindex="2" data-bind="options:LocationList, optionsText:'CountryName', optionsValue:'Id', value:Location"></select>

查看控制台日志时,结果如下:


结果是选择选项中没有数据。有没有关于如何以正确的方式做到这一点的建议?谢谢

我会这样做:

// Create an object 
var Country = function (Id, Name) {
        self = this;
        self.Id = Id;
        self.CountryName = Name;
    }

// Create a mapping object
    var mapping = {
        'LocationList': {
            create: function(options) {
                return new Country(options.data.Id, options.data.CountryName);
            }
        }
    }

// Create the view model
function AViewModel()
{
  var self = this;  
    self.LocationList = ko.observableArray();
    self.Location = ko.observable("2");

    // Map the json to the view model
    $.ajax({
       url:"/echo/json/",
       data:data,
       type:"POST",
       success:function(response)
       {
         self.LocationList = ko.mapping.fromJS(response, mapping, self);
       }
    });


}

var viewModel = new AViewModel();
ko.applyBindings(viewModel);
带有模拟json调用的JSFIDLE:


选择选项中无数据是什么意思?如果打开下拉列表,其中没有项目?或者您的当前位置未预选?下拉列表中没有项目。你的权利我的意思是你的代码没有任何问题。我想self.LocationList是一个可观察的工具。不明显。你能不能稍微修改一下模拟数据?这将更容易调试。是的,这是一个可观察的方式。好的,我会为它制作一把小提琴