1

I'm having trouble binding a basic list of values to the model in my post action in the controller. The list is returning null.

I have a viewmodel which looks like this:

public class DistributionListFormModel : FormViewModel
{
    [Required(ErrorMessage = "The title is required")]
    public string Title { get; set; }

    public List<DistributionItem> DistributionItems { get; set; }
}

public class DistributionItem
{
    public string LabelText { get; set; }
    public bool Signup { get; set; }
}

Ok - so in my controller, I populate the DistributionItems list with some dud values within the index method. And here is a snapshot of my view:

<div class="o-wrapper">

@using (Html.BeginForm("Submit", "MyController", FormMethod.Post))
{
    foreach (var item in Model.DistributionItems)
     {
         <p>@item.LabelText</p>
         @Html.CheckBoxFor(m => item.Signup)

    }
    <button type="submit">SUBMIT</button>

}

Any idea why the DistributionItems list is null in the post controller? I found some articles but they all relate to very older versions of ASP.Net...

Thanks!!

2
  • 3
    It would really help to see your controller action. Commented Jul 26, 2017 at 19:50
  • Post the code where you're passing the model to the view and all about the model creation. Commented Jul 26, 2017 at 19:50

2 Answers 2

1

This is one of those maddening things that exposes the limitations of model binding with lists in MVC.

What it comes down to is that you have to use a for loop with an indexer - a foreach loop won't work:

for (int i = 0; i < Model.DistributionItems.Count; i++)
{
     <p>@Model.DistributionsItems[i].LabelText</p>
     @Html.CheckBoxFor(m => m.DistributionsItems[i].Signup)
}

Here are a few references related to this that go into some more detail:

Model Binding to a List MVC 4

ASP.NET MVC 5 model binding list is empty

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

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

6 Comments

Wow, two downvotes and no comments, did I miss something obviously wrong here?
@Pat There is absolutely something wrong with using a foreach. The HTML helpers do not generate unique indices in the rendered HTML, which means the model binder is not able to determine how the elements should be associated when binding to a collection.
@EricPetroelje No, your answer is correct. The reason I linked to the duplicate, though, is because people should understand how editor templates work, as they make this sort of situation so trivial to deal with.
@JohnH - Yup, editor templates are the best way to handle this scenario. Just didn't want to introduce additional concepts that might cloud the core issue that the OP is having here. Added my close vote as well.
Ah okay I see what you're saying, its only an issue with implicit binding then. Good to know -- I stand corrected
|
0

Have you tried doing this:

foreach (var item in Model)
{
    @Html.HiddenFor(model => item.LabelText )
    @Html.LabelFor(model=> item.LabelText )
    @Html.CheckBoxFor(model => item.Signup)
}

I've had this issue before and what's happening is your controller doesn't see the object. All you're returning is a list of boolean.

1 Comment

If this doesn't work you can always build your list with jQuery and post it with Ajax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.