1

Before I begin, I have looked at this question:

asp.net mvc model binding fails for <List> items in editor template

I have a model which looks as so:

public class PartnerListModel
{
    public List<PartnersModel> Partners { get; set; }

    public PartnerListModel()
    {
        Partners = new List<PartnersModel>();
    }
}

And then my PartnersModel looks as such:

public class PartnersModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

I'm passing the PartnersListModel to the view, which looks like this:

using (Html.BeginForm("Update", "Partners", FormMethod.Post))
            {
                @Html.EditorFor(m => m.Partners)
                <input type="submit" value="Submit Changes"/>
            }

And finally, my editor template looks like this:

@model AdminWebsite.Models.Partners.PartnersModel
<div>
    <span>
        @Html.DisplayFor(m => m.Name)
    </span>
    <span>
        @Html.EditorFor(m => m.IsActive)
    </span>
    @Html.HiddenFor(m => m.ID)
    @Html.HiddenFor(m => m.Name)
</div>

My controller's action is as so, and the code does actually manage to hit this action:

public ActionResult Update(PartnerListModel partners)

Why is it that my List inside the model has a count of 0? I can't find any reason why my example differs from an accepted answer on Stack Overflow. Is there anything that I am missing that would explain why my data values passed back are not being added to the list?

Using Chrome's developer tools I've been able to confirm that I have a list which looks similar to the following:

  • Partners[0].IsActive = true
  • Partners[0].IsActive = false
  • Partners[0].Name = "Hi"
  • Partners[0].ID = 1
  • Partners[1].IsActive = false etc.

Any ideas? Thanks in advance.

2
  • Partners[0].IsActive is twice? see your chrome result Commented May 14, 2013 at 21:42
  • Yes it's there twice. As per the MVC framework, CheckboxFor creates a hidden field with value false as well as an input field because it must post a false value if the box is not checked. Commented May 14, 2013 at 21:47

1 Answer 1

3

You're passing a List<PartnersModel> to the action method, but the action-method expects a PartnerListModel.

You should change the Action-method like this:

public ActionResult Update(List<PartnersModel> partners)

The reason for this is that the default ModelBinder doesn't actually nest the graph. If you would want access to both items, you would need to do include both parameters:

public ActionResult Update(PartnerListModel partnerlistmodel, List<PartnersModel> partners){

}

If you want a nested model, you'd have to implement your own modelbinder.

The following question has a similar problem: Binding an editable list of children

Sign up to request clarification or add additional context in comments.

4 Comments

Suppose he wanted to return the PartnerListModel ViewModel in its entirety, what would have to change to result to that?
You were spot on! Why am I passing a List<PartnersModel> though? Also I should note that the action-method parameter NEEDS to be called partners in this case or you will get a no parameterless constructor error. I'm leaving this here for future reference.
I'm not a 100% sure on that one, but I reckon that once it sees that there's no other data in the request, it leaves out the root object. Not 100%, will investigate now and I will come back with my findings
OK, got it now. I updated my answer with some more information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.