Dynamic Add Row to MVC View then pass to controller

ajax, asp.net-mvc-3, c#, controller, partial-views

Solution

I would highly recommend reading Phil Haacked - Model Binding to a List. His example could easily be extended to use Ajax.

Problem

Okay, so I've tried a ton of things, none of which I'm very familiar with, and I can't seem to figure out exactly how to get done what I need to do. It seems like a simple task, and I'm sure someone has done it! In essence, I need to create a form that has the ability to create more fields that will bind to the model, or even just pass to the controller on submit. First, I have a model that is pretty straightforward. Something like: ``` public class Account { public string Organization { get; set; } public List<UserModel> Users { get; set; } } public class UserModel { public string UserFirstName { get; set; } public string UserLastName { get; set; } public string UserEmailAddress { get; set; } public bool UserIsAdmin { get; set; } } ``` My view is a simple signup form that takes all the input necessary to create an account. It needs to have these fields, but also be able to have a link that says "add User" and ajax in another row with first name, last name, email, and isAdmin flag. This is where I get stuck. I'm thinking I need something with a partial view and a custom binding to a controller as the data gets passed in, but I can't figure out how to pass in an object that contains all the users so I can iterate through it. If anyone could even point me in the right direction, I would be very happy. -Lost

Original source