简体   繁体   English

在Knockout.js和MVC 3中出错

[英]Get error in Knockout.js and MVC 3

this is my sample but not work and get this error in chrome Uncaught Error: Unable to parse binding attribute. 这是我的示例,但无法正常工作,并且在chrome中出现此错误未捕获的错误:无法解析绑定属性。 Message: ReferenceError: ProductName is not defined; 消息:ReferenceError:未定义产品名称; Attribute value: text: ProductName 属性值:文本:ProductName

Action Code: 动作代码:

    public ActionResult GetProducts()
    {
        var product = _db.Products;
        return Json(product, JsonRequestBehavior.AllowGet);
    }

Html : HTML:

    <table id="timesheets" class="table table-striped table-hover table-condensed">   
        <thead>
            <tr>
                <th>ProductName</th>
                <th>CategoryID</th>
                <th>UnitPrice</th>
                <th>Discontinued</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: viewModel.Products">
            <tr>
                <td data-bind="text: ProductName"></td>
                <td data-bind="text: CategoryID"></td>
                <td data-bind="text: UnitPrice"></td>
                <td data-bind="text: year"></td>
            </tr>
        </tbody>
    </table>

Javascript Code: JavaScript代码:

<script type="text/javascript">

    $(function () {
        viewModel.loadProducts();
        ko.applyBindings(viewModel);
    });

    function Product(data) {
        this.ProductID = ko.observable(data.ProductID);
        this.ProductName = ko.observable(data.ProductName);
        this.CategoryID = ko.observable(data.CategoryID);
        this.UnitPrice = ko.observable(data.UnitPrice);
        this.Discontinued = ko.observable(data.Discontinued);
    }

    var viewModel = {

        Products: ko.observableArray([]),

        loadProducts: function () {

            var self = this;
            $.getJSON(
                '/Home/GetProducts',
                function (products) {
                    self.Products.removeAll();

                    $.each(products, function (index, item) {
                        self.Products.push(new Product(item));
                    });
                }
            );
        }
    };

</script>

please Help, thanks 请帮助,谢谢

Please check attributes/properties in _db.Products . 请检查_db.Products属性/属性。 They have not found ProductName may be it is missing. 他们尚未找到可能缺少的ProductName If you are using EF then refresh your EntityFramework model again. 如果您使用的是EF,请再次刷新EntityFramework模型。 Or you can also check whether ProductName is empty or null by using alert like: 或者,您也可以使用以下警报来检查ProductName是空还是空:

function Product(data) {
        alert(data.ProductName);
        this.ProductID = ko.observable(data.ProductID);
        this.ProductName = ko.observable(data.ProductName);
        this.CategoryID = ko.observable(data.CategoryID);
        this.UnitPrice = ko.observable(data.UnitPrice);
        this.Discontinued = ko.observable(data.Discontinued);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM