MVC4 WebApi将JSON敲除为';在';

MVC4 WebApi将JSON敲除为';在';,json,knockout.js,asp.net-mvc-4,asp.net-web-api,runtime-error,Json,Knockout.js,Asp.net Mvc 4,Asp.net Web Api,Runtime Error,Hy,我被这个错误消息困扰,我找不到解决方案 我在Knockout JavaScript库v2.2.0中收到以下消息错误: 中第1053行第5列的未处理异常 localhost:port/Scripts/knockout-2.2.0.debug.js 0x800a138f- Microsoft JScript运行时错误:“in”的操作数无效:对象 如果存在此异常的处理程序,则该程序可能是 安全地继续 它在knockout-2.2.0.debug.js中的这一行代码处停止 if ((initia

Hy,我被这个错误消息困扰,我找不到解决方案

我在Knockout JavaScript库v2.2.0中收到以下消息错误:

中第1053行第5列的未处理异常 localhost:port/Scripts/knockout-2.2.0.debug.js 0x800a138f- Microsoft JScript运行时错误:“in”的操作数无效:对象 如果存在此异常的处理程序,则该程序可能是 安全地继续

它在knockout-2.2.0.debug.js中的这一行代码处停止

 if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues)) 
我使用这个WebApi:

public class ProductsController : ApiController
{
  IEnumerable<Product> products = new List<Product>() 
    { 
        new Product { Id = 1, Name = "Tomato_Soup", Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
    };

      public IEnumerable<Product> GetAllProducts(){
            return products.AsEnumerable();    }
公共类产品控制器:ApiController
{
IEnumerable products=新列表()
{ 
新产品{Id=1,Name=“番茄汤”,Category=“杂货”,Price=1},
新产品{Id=2,Name=“Yo”,Category=“Toys”,Price=375万},
新产品{Id=3,Name=“Hammer”,Category=“Hardware”,Price=16.99M}
};
公共IEnumerable GetAllProducts(){
返回产品。AsEnumerable();}
我使用的脚本在标题部分

@section Testscripts
{
    <script src="~/Scripts/jquery-1.8.2.js"></script>
    <script src="~/Scripts/knockout-2.2.0.debug.js"></script> 


}
@节测试脚本
{
}
以及页脚默认脚本部分中的淘汰代码

@section scripts
{
    <script type="text/javascript">      
        var apiUrl = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" })';  

        function Product(data) {            
            this.Id = ko.observable(data.Id);
            this.Name = ko.observable(data.Name);
            this.Price = ko.observableArray(data.Price);
            this.Category = ko.observable(data.Category);

        }

        function ProductViewModel() {

            var self = this;
            self.myproducts = ko.observableArray([]);


        $.getJSON(apiUrl, function (allData) {
            var mappedProducts = $.map(allData, function (item) { return new Product(item) });

            self.myproducts(mappedProducts);

        });
      };
   ko.applyBindings(new ProductViewModel);
}
@节脚本
{
var apiUrl='@Url.RouteUrl(“DefaultApi”,新的{httproute=“”,controller=“products”})”;
函数积(数据){
this.Id=ko.observable(data.Id);
this.Name=ko.observable(data.Name);
this.Price=ko.observearray(data.Price);
这个.Category=ko.可观察(data.Category);
}
函数ProductViewModel(){
var self=这个;
self.myproducts=ko.observearray([]);
$.getJSON(apiUrl,函数(allData){
var mappedProducts=$.map(所有数据,函数(项){返回新产品(项)});
self.myproducts(mappedProducts);
});
};
ko.applyBindings(新产品视图模型);
}
并在正文中显示数据:

<ul data-bind="foreach: myproducts">
    <li>
        <input data-bind="value: Id" />
        <input data-bind="value: Name" />
        <input data-bind="value: Category" />
        <input data-bind="value: Price" />
    </li>
</ul>

该缺陷存在于您的
产品功能中

您想从
data.Price
创建一个
ko.observableArray
,它是一个十进制值,而不是一个值数组,这导致了这个不太好的异常

更改为
ko.observable
,它应该可以工作:

function Product(data) {            
        this.Id = ko.observable(data.Id);
        this.Name = ko.observable(data.Name);
        this.Price = ko.observable(data.Price);
        this.Category = ko.observable(data.Category);

    }

是的,这就是问题所在