asp.net mvc 2. Adding to a viewModel's collection(client side)
I know that you can bind to a viewModel's collection on the client side like so:
<%for(int i = 0; i < Model.contacts.Count; i ++){%>
First Name: <%: Html.TextBoxFor(model => model.contacts[i].firstName) %>
Last Name: <%: Html.TextBoxFor(model => model.contacts[i].lastName) %>
<%}%>
... this will allow the user to change the name info associated with this collection and when the form is posted to the receiving action the viewModel will have the respective changes.
Gi开发者_JS百科ven that I can edit a viewModel's collection from the client, is there a way I can add to a viewModel's collection from the client as well. For example add a new contact into the viewModel's contacts List. I want to put a simple "Add contact" button in my page that will allow the user to add a contact to this list with out going back and forth to the server. Am i trying to do something impossible. I hope this makes sense. Thanks.You may checkout the following blog post from Steve Sanderson which exposes a nice approach to achieve what you are looking for. Also instead of writing the code you've shown in your question I would recommend you using Editor Templates and then simply replace it with:
<%= Html.EditorFor(x => x.contacts) %>
精彩评论