Binding list of objects with Knockoutjs

自闭症网瘾萝莉.ら 提交于 2019-12-11 04:53:51

问题


I'm trying to bind data to ASP.NET Webforms Application using KnockoutJS and Knockout Mapping

HTML

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.3.0.js" type="text/javascript"></script>
<script src="Scripts/knockout.mapping-latest.js" type="text/javascript"></script>
<script type="text/javascript">

    function bindModel(data) {
        var viewModel;
        viewModel = ko.mapping.fromJS(data);
        console.log(viewModel);
        ko.applyBindings(viewModel);
    }

    $(document).ready(function () {
        $.ajax({
            url: "TestPage.aspx/GetItems",
            data: {},
            type: "POST",
            contentType: "application/json",
            dataType: "JSON",
            timeout: 10000,
            success: function (result) {
                bindModel(result);
            },
            error: function (xhr, status) {
                alert(status + " - " + xhr.responseText);
            }
        });
    });
</script>
...
<table>
    <thead>
        <tr>
            <th>
                Id
            </th>
            <th>
                Name
            </th>
        </tr>
    </thead>
    <tbody data-bind="foreach: Item">
        <tr>
            <td data-bind="text: Id">
            </td>
            <td data-bind="text: Name">
            </td>
        </tr>
    </tbody>
</table>

C#

[WebMethod]
public static List<Item> GetItems()
{
    List<Item> itemlist = new List<Item>
        {
            new Item {Id = 1, Name = "Item1", Description = "Item 1 Description"},
            new Item {Id = 2, Name = "Item2", Description = "Item 2 Description"}
        };

    return itemlist;
}

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

}

JSON Response

{"d":[{"__type":"KnockoutWebFormsTest.Item","Id":1,"Name":"Item1","Description":"Item 1 Description"},{"__type":"KnockoutWebFormsTest.Item","Id":2,"Name":"Item2","Description":"Item 2 Description"}]}

But this gives an error,

Uncaught ReferenceError: Unable to parse bindings.
Bindings value: foreach: Item
Message: Item is not defined

What am I doing wrong here, How to fix this?


EDIT :

If I call bindModel directly with data, like

bindModel({ "d": [{ "__type": "KnockoutWebFormsTest.Item", "Id": 21, "Name": "Item1", "Description": "Item 1 Description" }, { "__type": "KnockoutWebFormsTest.Item", "Id": 2, "Name": "Item2", "Description": "Item 2 Description"}] });

and change data-bind="foreach: Item" to data-bind="foreach: d" (As suggested by david.s)

It works fine... But if I pass JSON result directly to bindModel it gives the error

d is not defined

Any idea how to fix this Issue?


回答1:


From your JSON response {"d":[ ... ]} I can see that the array is called d. So your binding should be foreach: d.




回答2:


Just a guess but you are not passing the array to be bound. You are passing the json object. {d:[{foo:bar},{foo:bar},]}

You could change this line bindModel(result); to bindModel(result.d); in order to access the array that "d" contains.

I have created a JSBin.. http://jsbin.com/uxikew/2/edit

The ajax call has been removed but you get the point.




回答3:


Thanks for all the answers and help...

Finally Managed to get it working,

by changing data-bind="foreach: Item" to data-bind="foreach: d" (as sugested by david.s)

and viewModel = ko.mapping.fromJS(data); to viewModel = ko.mapping.fromJSON(data);



来源:https://stackoverflow.com/questions/18008743/binding-list-of-objects-with-knockoutjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!