Skip to main content
 首页 » 编程设计

c#之Knockout 选择显示 optionsText 的对象对象

2024年02月13日13xiaohuochai

我有一个对象的 json 列表,我已经填充到一个 observablearray([]) 中。我知道数据在那里,因为如果我这样做:

<table id="tblColors"> 
<thead> 
    <tr> 
        <th padding: 10px; >Color</th> 
    </tr> 
</thead> 
<tbody data-bind="foreach: AllColors"> 
    <tr> 
        <td data-bind="text: ColorName"></td> 
    </tr> 
</tbody> 

View 显示所有颜色。

但是,当我这样做时:

<select data-bind="options: AllColors, optionsText: AllColors.ColorName,  
    value: AllColors.ID, optionsCaption: 'Select Color...'"></select>  

我在下拉框中得到默认的“选择颜色...”,但每种颜色都有 [object Object]

我试过 optionsText: ColorName 但程序停止并提示 JavaScript 运行时错误:'ColorName' 未定义。

请您参考如下方法:

这样试试

  <select data-bind="options: AllColors, optionsText: 'ColorName', value: selectedColor, optionsCaption: 'Choose...'></select 

单引号 (') 是秘诀。