使用 KnockoutJS 和 MVC 3.0 的嵌套模板
我是淘汰赛 JS 的新手,但很享受每天学习的每一点。
这是我的问题。根据加载和保存数据教程,假设我的 MVC 3.0 视图模型中有以下类:
public class MasterModel
{
public int Id { get; set; }
public string Description { get; set; }
public ICollection<ParentModel> Parents { get; set; }
}
public class ParentModel
{
public int Id { get; set; }
public string Description { get; set; }
public ICollection<ChildModel> Children { get; set; }
}
public class ChildModel
{
public int Id { get; set; }
public string Description { get; set; }
}
我的 HomeController 的 Index() 方法返回一个 MasterModel 实例,其中包含一个 ParentModel 列表,每个实例又包含一个 ChildModel 列表。在客户端,我有以下视图:
@model SomeNamespace.Models.MasterModel
(...)
<script type="text/javascript">
var initialData = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));
var viewModel = {
parents: ko.observableArray(initialData.Parents),
(...)
};
我希望能够使用嵌套模板来显示绑定到 MasterModel 的 ParentModel 列表,以及每个 ParentModel 的 ChildrenModel 列表。我还希望两个列表(ParentModel 和 ChildrenModel)都是可观察数组,以便可以动态添加或删除每个列表的项目。
我尝试按照 Knockout JS 网站上的 "template" 绑定 文章来实现此操作,但我不确定如何实现实现包含可观察数组列表的可观察数组...
有人可以指出我正确的方向吗?
提前致谢!
I am new to knockout JS, but am enjoying every bit that I am learning each day.
Here is my question. Based on the Loading and saving data tutorial, let's say that I have the following classes in my MVC 3.0 view model:
public class MasterModel
{
public int Id { get; set; }
public string Description { get; set; }
public ICollection<ParentModel> Parents { get; set; }
}
public class ParentModel
{
public int Id { get; set; }
public string Description { get; set; }
public ICollection<ChildModel> Children { get; set; }
}
public class ChildModel
{
public int Id { get; set; }
public string Description { get; set; }
}
And that my Index() method of the HomeController returns an instance of MasterModel with a list of ParentModel, each in turn containing a list of ChildModel. On the client side, I have the following view:
@model SomeNamespace.Models.MasterModel
(...)
<script type="text/javascript">
var initialData = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));
var viewModel = {
parents: ko.observableArray(initialData.Parents),
(...)
};
I would like to be able to use nested templates to display the list of ParentModel bound to the MasterModel and for each ParentModel, the list of ChildrenModel. I also want both lists (ParentModel and ChildrenModel) to be observable arrays so that items of each list can be added or removed dynamically.
I have attempted to implement this following the "template" binding article on the Knockout JS site, but am not sure how to implement the observable array containing a list of... observable arrays...
Can someone point me to the right direction?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 jsFiddle 示例 应该能够帮助您,它的工作原理如下:
这段代码:
This jsFiddle example should be able to help you on the way, it works like this:
And this code: