用ASP.Net MVC淘汰 - 数据映射问题

时间:2013-12-26 16:37:15

标签: c# asp.net asp.net-mvc knockout.js

我的观点模型看起来像这样 -

class ParentVM
{
    public List<Level1ChildVM> Level1Children();
    public class Level1ChildVM
    {
        public string Name;
        public Level2ChildVM Level2Child;
        public Class Level2ChildVM { .. level2 child properties goes here }
    }
}

在我看来,我使用虚拟元素中的foreach绑定迭代Level1Children集合 -

js code -

 dataService.getData(function (data) {
        //bind data to form elements
        myVM = ko.mapping.fromJS(data);

        ko.applyBindings(myVM);
    });

我面临的问题是level1ChildVM中的属性将显示在视图中。但是不会显示level2子级的属性。 我也尝试过使用绑定但是id不起作用。

我正确应用绑定吗? 请帮忙。

1 个答案:

答案 0 :(得分:0)

你的js代码是这样的

Level1ChildMain = ko.observable({ arrayList: [{ id: 1, Name: "AA" }, { id: 2, Name: "BB" }, { id: 1, Name: "CC"}], single: { id: 10, Name: "Outer"} });

你的Html就像这样

<div>
    <br />
    <div data-bind="foreach : Level1ChildMain.arrayList">
        <div>
            <label data-bind="value : id"></label>

        </div>
    </div>
</div>
<div>
    <br />
    <div data-bind="with : Level1ChildMain.single">
        <div>
            <label data-bind="value : id"></label>
        </div>
    </div>
</div>

此外,您可以将此link引用为两级模型绑定