mituw16
mituw16

Reputation: 5250

Model Binder null on List<object> sometimes

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

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();
}

Upvotes: 4

Views: 1396

Answers (1)

mituw16
mituw16

Reputation: 5250

I have not been able to find a coding solution to this problem, and started exploring other avenues. I have fixed the problem, but it makes very little sense.

I upgraded the project from .net core 1.0.1 to .net core 1.0.5 , and everything is working 100% of the time. In this upgrade, I made no code changes what so ever. Just upgraded .net core. Very very odd..

Upvotes: 1

Related Questions