It is impossible to understand how to implement a knockout for the following ASP.NET MVC 4 submission model:
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; } }
In the view, I accept the model as follows:
@model MyProfile @using (Html.BeginForm("Index", "Profile", FormMethod.Post, new { id = "profileEditorForm" })) { @Html.ValidationSummary(false) <fieldset> <legend>User 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> }
The EditorTemplate for List1 will look like this:
@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>
Edit
VM1 through VM10 are the same except for the verification logic, so I have to make them different classes (unfortunately, since these are TONS repetitions in models and in representations).
The client side is what I ask: I need to switch from ASP MVC models containing nested lists and present them on the client with a knockout (I found that it is best suited for dynamic lists). This is something similar to gmail contacts - you have a home / work / mobile / fax phone, so one list is a shortcut for the phone (which phone), and it should be presented as combobox, the other is dynamic lists of phones that may increase as the user clicks.
End editing
I donโt understand how to create a knockout of viewModel from this nested model, it is obvious that Name should be part of this, but the rest are lists, and they also contain lists.
How to match it?
how to handle it (one is on the drop-down list, which will be a shortcut to another list, which is variable length - the only reason for using knockout here)
after filling, how to put it all together and send it back to the controller?
how to write this editor model when the label is a drop-down list (or combobox) of labels for the name (example: [label] home / work [name] email, [label] mobile / home / car [name] phone)
If it was a simple class with an IList inside - it's like here . The problem is that there are lists inside the lists, knockout requires that everything is observed, not sure how to express this nesting model in a java script.
Please, help. Thanks in advance!