结合与Knockoutjs对象名单(Binding list of objects with Kno

2019-10-18 10:59发布

我试图绑定使用数据ASP.NET Web表单应用KnockoutJS和淘汰赛制图

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

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

但是,这给出了一个错误,

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

我在做什么错在这里,如何解决这一问题?


编辑:

如果我叫bindModel直接与数据,如

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

和变化data-bind="foreach: Item"data-bind="foreach: d" (如所建议的通过david.s)

它工作正常......但是,如果我直接传递JSON结果bindModel它给人的错误

d is not defined

任何想法如何解决这个问题?

Answer 1:

从您的JSON响应{"d":[ ... ]}我可以看到数组被称为d 。 所以,你应该结合是foreach: d



Answer 2:

只是一种猜测,但你不通过数组的约束。 您传递JSON对象。 {d:[{foo:bar},{foo:bar},]}

你可以改变这一行bindModel(result);bindModel(result.d); 为了访问阵列“d”中含有。

我创建了一个JSBin .. http://jsbin.com/uxikew/2/edit

Ajax调用已被删除,但你明白了吧。



Answer 3:

感谢所有的答案,帮助...

终于设法得到它的工作,

通过改变data-bind="foreach: Item"data-bind="foreach: d" (由david.s sugested)

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



文章来源: Binding list of objects with Knockoutjs