Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckboxFor not binding with nested objects

CheckBoxFor is not bounded when a property is defined in an object nested in the model?

Here is an example. I have a SearchOptions model that contains a List<Star> property. Each Star has a number, a name and a bool property that should be bounded:

public class SearchOptions
{

    public SearchOptions()
    {
        // Default values
        Stars = new List<Star>()
        {
            new Star() {Number=1, Name=Resources.Home.Index.Star1,
                IsSelected=false},
            new Star() {Number=2, Name=Resources.Home.Index.Star2,
                IsSelected=false},
            new Star() {Number=3, Name=Resources.Home.Index.Star3,
                IsSelected=true},
            new Star() {Number=4, Name=Resources.Home.Index.Star4,
                IsSelected=true},
            new Star() {Number=5, Name=Resources.Home.Index.Star5,
                IsSelected=true},
        };
    }

    public List<Star> Stars { get; set; }

}

In my strongly typed View (of SearchOptions) i loop over Stars property:

@using (Html.BeginForm("Do", "Home"))
{
    <fieldset>
        <legend>@MVC3TestApplication.Resources.Home.Index.Search</legend>
        @{ 
            foreach (Star s in Model.Stars)
           {
                @Html.CheckBoxFor(m => s.IsSelected)
                <label>@s.Name</label>

           }}
    </fieldset>
    <input type=submit value="Invia" />
}

The (relevant part of) controller is:

    public ActionResult SearchOptions()
    {
        return View(new SearchOptions());
    }

    [HttpPost]
    public ActionResult Do(SearchOptions s)
    {
        // Do some stuff
        return View("SearchOptions", s);
    }
like image 779
gremo Avatar asked Jul 21 '11 19:07

gremo


1 Answers

It's because of how you're accessing the properties in the CheckBoxFor expression.

@for (int i = 0; i < Model.Stars.Count(); i++) { 
    @Html.CheckBoxFor(m => m.Stars[i].IsSelected)
    <label>@Model.Stars[i].Name</label>
}

This should work for you.

Here's the output from the different methods:

//using the for loop
<input id="Stars_2__IsSelected" name="Stars[2].IsSelected" type="checkbox" value="true" />

//using the foreach
<input checked="checked" id="s_IsSelected" name="s.IsSelected" type="checkbox" value="true" />

You'll notice that the for foreach doesn't contain the proper name for it to match to when doing model binding.

like image 124
Buildstarted Avatar answered Oct 01 '22 10:10

Buildstarted