Asp.net mvc 在Knockout.js和MVC 3中获取错误
这是我的示例,但不工作,在chrome中出现此错误 未捕获错误:无法分析绑定属性。 消息:ReferenceError:未定义ProductName; 属性值:text:ProductName 行动代码:Asp.net mvc 在Knockout.js和MVC 3中获取错误,asp.net-mvc,knockout.js,Asp.net Mvc,Knockout.js,这是我的示例,但不工作,在chrome中出现此错误 未捕获错误:无法分析绑定属性。 消息:ReferenceError:未定义ProductName; 属性值:text:ProductName 行动代码: public ActionResult GetProducts() { var product = _db.Products; return Json(product, JsonRequestBehavior.AllowGet); }
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>
$(函数(){
viewModel.loadProducts();
应用绑定(视图模型);
});
功能产品(数据){
this.ProductID=ko.observable(data.ProductID);
this.ProductName=ko.observable(data.ProductName);
this.CategoryID=ko.可观察(data.CategoryID);
this.UnitPrice=ko.可观察(data.UnitPrice);
本.中断=可观察到的ko(数据.中断);
}
var viewModel={
产品:ko.observearray(【】),,
loadProducts:函数(){
var self=这个;
$.getJSON(
“/Home/GetProducts”,
功能(产品){
self.Products.removeAll();
$。每个(产品、功能(索引、项目){
自行产品推送(新产品(项));
});
}
);
}
};
请帮助,谢谢请检查
\u 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);
}
获取产品操作返回数据到json请求,我选中此项,返回数据为:[{“ProductID”:1,“ProductName”:“Chai”,“SupplierID”:1,“CategoryID”:1,“QuantityPerUnit”:“10个盒子x 20个袋子”,“单价”:18.0000,“UnitsInStock”:39,“UnitsOnOrder”:0,“ReorderLevel”:10,“中断”:false}]现在将警报放入
$.getJSON('/Home/GetProducts',函数(产品){alert(products.ProductName);self.products.removeAll();$.each(产品,函数(索引,项目){self.products.push(新产品(项目));});});
并查看是否成功获取了ProductName属性中的数据。请查看此线程。这可能是绑定问题:数据接收正常,我认为我的脚本或html不正常。在viewmodel中使用此选项:var self=this;self.task=ko.observable();
我认为这应该有效。。