I have a view model,and one of the properties of the view model is a an object called Profile. One of the properties of Profile is a list of another object, called CD. From the view, I set the POST body values to the following
Profile.CD[0].Prop1=TEST&Profile.CD[0].Prop2=TEST&Profile.CD[1].Prop1=TEST2&Profile.CD[1].Prop2=TEST2
If I were to add a third object to the list in the view, which would get posted as Profile.CD[2].Prop1=TEST3 , in the controller all of the sudden Profile.CD is null. 2 items and below Profile.CD gets the values I would expect. As soon as I add in that third item the model binder stops working. I'm at my wits end, and I've tried everything I can think of.
Things I've tried
Removing an item from the view, and adding a new -- WORKS
Removing both from the view, and adding 2 new items -- WORKS
Adding a third item in the view -- FAILS
Profile.CDis null in the view model
I'm not using any model state validation rules. When debugging, I've tried something like the following in the immediate window ?Request.Form.Where(x => x.Keys.Contain("Profile.CD")).ToList()) and sure enough, all of my values are present in the Request object even though the list is null in the view model.
The values of the objects in Profile.CD do not have to be unique.. I've tried setting every single value to "TEST" just to verify it's not the input causing this problem.
I'm truly lost..
View Model
public class PortalViewModel {
public ProfileModel Profile { get; set; }
}
Profile Model
public class ProfileModel {
//bunch of other static properties that are bound just fine.. like strings and decimals...
public List<CDModel> CD { get; set; }
}
Controller
public async Task<IActionResult> Edit (PortalViewModel Model)
{
Repo.UpdateProfile(Model.Profile); // in this method it foreachs Profile.CD , but sometimes this is null and other times it get's it's values....
return Ok();
}