asp.net-mvc – 具有嵌套视图模型和Knockout的ASP.NET MVC
无法让我的大脑围绕如何为以下ASP.NET MVC 4嵌套视图模型实现knockout:
public class MyProfile { public string Name { get; set; } public IList<VM1> List1 { get; set; } public IList<VM2> List2 { get; set; } .... public IList<VM10> List10 { get; set; } } // example of VM view model public class VM1 { IList<Label> Labels { get; set; } IList<Contact1> Contact1 { get; set; } } 在视图中我接受这样的模型: @model MyProfile @using (Html.BeginForm("Index","Profile",FormMethod.Post,new { id = "profileEditorForm" })) { @Html.ValidationSummary(false) <fieldset> <legend>User's data</legend> <label for="name">Name:</label> <input type="text" id="name" name="name" class="required" data-bind="value: Name"/> </fieldset> @Html.EditorFor(m => @Model.List1,"List1") @* Editpr model for List1*@ @Html.EditorFor(m => @Model.List2,"List2") ..... @Html.EditorFor(m => @Model.List10,"List10") <p> <input type="submit" value="Save" data-bind="enable: (List1().length > 0) && (List2().length > 0) && ...(List10().length > 0)" /> <a href="/">Cancel</a> </p> } List1的EditorTemplate看起来像这样,有多个问题: @model IList<FiveW.ViewModels.List1> <fieldset> <table> <tbody data-bind="foreach: Contact1"> <tr> <td> <label>Email:</label></td> <td> @* How do you put combobox here with labels here? How do you tie selected label to selected property on your Contact1 object *@ @*<select data-bind="options: Labels,optionsText: 'LabelName',value: selectedLabel,optionsCaption: 'Choose...'"></select></td> <td> <input type="text" data-bind="value: Name,uniqueName: true" class="required" /></td> <td> <a href="#" data-bind="click: function() { viewModel.removeContact1(this); }">Delete</a></td> </tr> </tbody> </table> <button data-bind="click: addContact1">Add Contact1</button> </fieldset> 编辑 除了验证逻辑之外,VM1到VM10是相同的,所以我必须让它们成为不同的类(不幸的是,因为它在模型和视图中重复了很多). 客户方 – 这就是我要问的问题: 结束编辑 >我不明白如何从这个嵌套模型创建一个knockout viewModel,显然Name应该是它的一部分,但其余的是列表,它们也包含列表. 如果它是IList里面的简单类 – 它就像here.问题是列表中有列表,Knockout要求一切都是可观察的,不知道如何在java脚本中表达这个嵌套模型. 请帮忙.提前致谢! 解决方法
我不会使用映射.我只是像这样声明ViewModel客户端:
//I'm asuming the properties for Label and Contact,this is just for example purposes function LabelViewModel(){ var self = this; self.LabelName = ko.observable(); } function Contact(){ var self = this; self.Name = ko.observable(); self.LastName = ko.observable(); } //This is the definition for the List. I believe it shouldn't matter that the class names are different as long as the structure is the same function ListViewModel(){ var self = this; self.Labels = ko.observableArray(); self.Contacts = ko.observableArray(); } function MainViewModel(){ var self = this; self.Name = ko.observable(); self.List1 = ko.observableArray(); //.... self.List10 = ko.observableArray(); } $(document).ready(function(){ var viewModel = new MainViewModel(@Html.Raw(JsonConvert.SerializeObject(Model))); ko.applyBindings(viewModel); }); 然后,在提交时,我会尝试从jquery提交,而不是直接进行http post: var viewModelJs = ko.toJS(ko.utils.unwrapObservable(viewModel)); var parameters = JSON.stringify({vm: viewModelJs}); $.ajax('http://yourControllerUrlHere',{ data: parameters,type: "POST",contentType: "application/json",dataType: "json",success: function (result) { console.log('Submitted ok'); },error: function (error) { console.log(error); } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – Web.Config转换自定义配置部分
- asp.net-mvc – 在使用ORM解决方案的ASP.NET MVC中工作时,我
- 锁定ASP .Net自定义成员资格提供程序中的用户
- asp.net-mvc – 如何使用ASP.net MVC实现动态面包屑?
- asp.net-mvc – Sql Views的流畅Nhibernate映射
- asp.net-mvc – 用于ASP.NET MVC的Spark T4模板
- asp.net-mvc – ASP.Net WebAPI区域支持
- asp.net-mvc – razor要求;在做什么时使用(Html.BeginForm(
- ASP.NET MVC 4 EF5与MySQL
- asp.net-mvc-4 – ASP.NET MVC 4会员“记住我”如何运作?