[英]Get error in Knockout.js and MVC 3
這是我的示例,但無法正常工作,並且在chrome中出現此錯誤未捕獲的錯誤:無法解析綁定屬性。 消息:ReferenceError:未定義產品名稱; 屬性值:文本: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>
請幫助,謝謝
請檢查_db.Products
屬性/屬性。 他們尚未找到可能缺少的ProductName
。 如果您使用的是EF,請再次刷新EntityFramework模型。 或者,您也可以使用以下警報來檢查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.