1

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.
Given 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.

1 Answer 1

1

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) %>
Sign up to request clarification or add additional context in comments.

2 Comments

Darin. Thanks!, that's awesome! Saved me a ton of time. That's cool how the returned partial is automatically added to the model's collection too.
That's not really client-side

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.