Consider these two view models
public class PersonViewModel
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public int PersonAge {get; set;}
public virtual PersonJobSplitViewModel JobSplit { get; set; } // hold each split
public virtual List<PersonJobSplitViewModel> JobSplits { get; set; } //contain all splits
}
public class PersonJobSplitViewModel
{
public int PersonJobSplitId { get; set; }
public int PersonId { get; set; }
public string JobRole { get; set; }
public decimal SplitPercentage { get; set; }
public virtual PersonViewModel PersonViewModel { get; set; }
}
Each person can have between 1 - 3 jobs.
I have a Create view which is bound to my PersonViewModel
and in my controller's GET method I am creating an instance of List<PersonJobSplitViewModel>
with a capacity of 3 and assigning it to PersonViewModel.JobSplits
@model MySolution.Web.ViewModels.PersonViewModel
...
@for (var i = 0; i < Model.JobSplits.Capacity; i++)
{
@Html.EditorFor(model => model.JobSplit.JobRole);
@Html.EditorFor(model => model.JobSplit.SplitPercentage);
}
This results in the role and percentage inputs being rendered to the view 3 times. My POST method is expecting a PersonViewModel
however PersonViewModel.JobSplits
is coming in as null
. The JobSplit
property contains one of my 3 splits, as I kind of expect.
So how do I post the model bound with it's full list of JobSplits through to the controller?
I've found similar things have been answered previously but I cant seem to find a straight forward solution that is relevant to MVC5 and relates to tagging a list to a large model as apposed to just passing a list to the controller.
Update
I've now tried doing the following
@for (var i = 0; i < Model.JobSplits.Capacity; i++)
{
@Html.EditorFor(model => model.JobSplits[i].JobRole);
@Html.EditorFor(model => model.JobSplits[i].SplitPercentage);
}
But I get:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index