json - MVC4 WebApi Knockout JSON 无效操作数为 'in'

标签 json knockout.js asp.net-mvc-4 asp.net-web-api runtime-error

嘿,我被这个错误消息困住了,而且找不到解决方案。

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

Unhandled exception at line 1053, column 5 in localhost:port/Scripts/knockout-2.2.0.debug.js 0x800a138f - Microsoft JScript runtime error: Invalid operand to 'in': Object expected If there is a handler for this exception, the program may be safely continued.

它停在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();    }

我使用的脚本位于标题部分

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


}

以及页脚默认脚本部分中的 Knockout 代码

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

并在正文中显示数据:

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

最佳答案

该错误存在于您的 Product 函数中。

您想要从 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);

    }

关于json - MVC4 WebApi Knockout JSON 无效操作数为 'in',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13468751/

相关文章:

java - JSON - 对象数组中的对象数组

javascript - 从 JSON 创建数组 - Javascript

html - jQuery Mobile DateBox 和 Knockout.js

jquery - 在 .NET MVC4 中使用 Ajax 进行部分 View 加载

asp.net-mvc - ASP.NET MVC Razor 串联

c# - ASP NET MVC 4 : How to return a file from a controller to view with angularJs?

json - 如何将对象转换为 Jooq JSON

json - 如何让 Jackson 使用单引号或不使用引号生成 json 字符串?

javascript - 在IE浏览器中 knockout TR标签之间的虚拟元素

knockout.js - 可 knockout 排序和掉落元素会引发控制台错误