【问题标题】:Get error in Knockout.js and MVC 3在 Knockout.js 和 MVC 3 中出现错误
【发布时间】:2013-04-04 16:45:16
【问题描述】:

这是我的示例,但无法正常工作并在 chrome 中出现此错误 未捕获的错误:无法解析绑定属性。 消息:ReferenceError:ProductName 未定义; 属性值:文本:ProductName

操作代码:

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

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 代码:

<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>

请帮忙,谢谢

【问题讨论】:

    标签: asp.net-mvc knockout.js


    【解决方案1】:

    请检查_db.Products 中的属性/属性。他们还没有找到ProductName 可能是它丢失了。如果您使用的是 EF,请再次刷新您的 EntityFramework 模型。或者您也可以使用如下警告来检查 ProductName 是否为空或 null:

    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);
        }
    

    【讨论】:

    • 获取产品操作返回数据到 json 请求,我检查了这个,返回数据是:[{"ProductID":1,"ProductName":"Chai","SupplierID":1,"CategoryID" :1,"QuantityPerUnit":"10 箱 x 20 袋","UnitPrice":18.0000,"UnitsInStock":39,"UnitsOnOrder":0,"ReorderLevel":10,"Discontinued":false}]
    • 好的,现在把alert放到$.getJSON( '/Home/GetProducts', function (products) { alert(products.ProductName); self.Products.removeAll(); $.each(products, function (index, item) { self.Products.push(new Product(item)); }); } );,看看ProductName属性中的数据是否成功获取..
    • 请看这个帖子可能是绑定问题:link
    • 数据接收正常,我认为我的脚本或 html 不正常
    • 在您的视图模型中使用它:var self = this; self.task = ko.observable(); 我认为这应该可行..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多