I have some viewModels similar to these:
public class ResturantViewModel
{
public ResturantViewModel()
{
Clients.Add(new ClientViewModel());
}
public string MyProperty {get;set;}
public IList<ClientViewModel> Clients = new List<ClientViewModel>();
}
public class ClientViewModel
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
In my View I have something like:
@foreach(var client in Model.Clients)
{
<tr>
<td>First Name: @Html.EditorFor(item => client.FirstName)</td>
<td>Last Name: @Html.EditorFor(item => client.LastName)</td>
</tr>
}
I’d like to have a button which can add some new blank ClientViewModels to the ResturantViewModel.Clients list so that it could be rendered in the View and posted to the Resturant/Create action.
Thanks in advance
You may take a look at the following blog post. It uses the WebForms view engine but it could be very easily adapted to Razor.