2
我的应用程序是MVC 5.我使用下面的淘汰赛剑道下拉列表:淘汰赛剑道DROPDOWNLIST获得所选项目的文本
<input data-bind="kendoDropDownList: { dataTextField: 'name',
dataValueField: 'id', data: foodgroups, value: foodgroup }" />
<hr />
Selected: <strong data-bind="text: foodgroup"> </strong>
<script>
var ViewModel = function() {
var self = this;
this.foodgroups = ko.observableArray([
{ id: "1", name: "apple" },
{ id: "2", name: "orange" },
{ id: "3", name: "banana" }
]);
var foodgroup =
{
name: self.name,
id: self.id
};
this.foodgroup = ko.observable();
ko.bindingHandlers.kendoDropDownList.options.optionLabel = " - Select -";
this.foodgroup.subscribe(function (newValue) {
alert(newValue.name);
});
};
ko.applyBindings(new ViewModel());
</script>
我试图让所选项目的文本。如果我使用alert(newValue),我得到了id,当我使用newValue.name或newValue.Text时,我得到了未定义的值。
谢谢马可,这是伟大的。是否可以将名称设置为可观察对象,并仅将名称绑定? – hncl
当我使用ajax填充下拉列表时:var data = db.FoodGroups.Select(c => new { id = c.FoodGroupID, name = c.FoodGroupName })。ToList(); 找不到名字? – hncl
我已经更新了问题http://stackoverflow.com/questions/35400673/knockout-kendo-dropdownlist-ajax-observablearray-get-selected-item-name – hncl