Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Knockout, Cannot use 'in' operator to search for 'length'

I am getting the following error on my web application using Knockout.js

Cannot use 'in' operator to search for 'length'

My Code:

$(document).ready(function () {
        AjaxRequest();
    });

    function AjaxRequest() {
        $.post("../../Api/DisabilitiesHandler.ashx?method=get", function (data) {
            var viewModel = {
                disabilities: ko.observableArray(data)
            };

            ko.applyBindings( viewModel, document.body);
        });
    }

<table>
    <tbody data-bind="template: { name: 'disabilitiesRowTemplate', foreach: disabilities }"></tbody>
</table>

<script type="text/html" id="disabilitiesRowTemplate">
    <tr>
        <td>Name:
            <input data-bind="value: Name" /></td>
        <td>
           Active <input type="checkbox" data-bind="checked: Active" /></td>
    </tr>
</script>

And this is my model

public class Disabilities
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}

And this is the code of the web service

context.Response.ContentType = "application/JSON";
DbsaDal.Entities.DBSAEntities db = new DbsaDal.Entities.DBSAEntities();
List<DbsaDal.Model.Disabilities> disabilities = DbsaDal.Entities.Disabilities.Get(db);
context.Response.Write(new JavaScriptSerializer().Serialize(disabilities));

Any suggestions on what to do? I have searched everywhere on the web and can't find anything useful

Update 1:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"Id":1,"Name":"Blind","Active":false},{"Id":2,"Name":"Mute","Active":true}] Knockout.js:92
like image 651
Armand Avatar asked Jul 05 '12 12:07

Armand


1 Answers

I found my problem:

disabilities: ko.observableArray(data)

This piece of code should have been

disabilities: ko.observableArray(ko.utils.parseJson(data))
like image 199
Armand Avatar answered Oct 27 '22 11:10

Armand