敲除下拉错误显示数据

时间:2015-01-13 18:11:16

标签: knockout.js

Select Code: <select data-bind="options: violations, optionsText: desc, optionsValue: violCd">      </select>

<ul data-bind="foreach: violations">
   <li><span data-bind="text: violCd"></span></li>
</ul>


@section scripts {

@Scripts.Render("~/bundles/knockout")
<script type="text/javascript">

    function violation(data) {
        var self = this;
        self.violCd = data.violCd;
        self.desc = data.desc;
        self.amt = data.amt;
    }

    function MyViewModel() {
        var self = this;
        self.violations = ko.observableArray([]);
        self.loadToList = ko.computed(function() {
            alert('selected');
        });
        $.ajax({
            url: "/Data/GetViolations",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data);
                $.each(data, function(i,val){
                    self.violations.push(new violation({ violCd: val.violCd, desc: val.desc, amt: val.amt }));
                });
            }
        });
    }
    ko.applyBindings(new MyViewModel());

</script>    

}

如果我注释掉select并只显示列表 - 那很好 - 但是当我测试select时,我得到一个“未捕获的ReferenceError:violCd未定义”错误。

感谢。

2 个答案:

答案 0 :(得分:0)

这解决了它:

Select Code: <select data-bind="options: violations, 
                            optionsText: function(item) {
                                return item.desc
                            }, 
                            optionsValue: function(item) { return item.violCd; }"></select>

答案 1 :(得分:0)

我认为您需要做的就是在这些参数周围添加引号: - &#39; desc&#39;和&#39; violCd&#39;

Select Code: <select data-bind="options: violations, optionsText: 'desc', optionsValue: 'violCd'">      </select>