Skip to content

CheckBoxListFor posted values weirdness #37

@Jogai

Description

@Jogai

There seems to be a bug in CheckBoxListFor concerning the values used after postback.
I cant get my posted values persist on the checkboxes, and I think it is because of the following inconsistency.
For the record, I have applied the fix from #30
The viewbag holds my data for the checkboxes (mind the selected = true!):

            ViewBag.Sectors = sectors.Select(sector =>
                new SelectListItem
                {
                    Text = sector.Name,
                    Value = sector.Id.ToString(),
                    Selected = true 
                })
            .ToList();

And the helper is using these values ...

@Html.CheckBoxListFor(model => model.SiteNewsSectors, ViewBag.Sectors as IEnumerable<SelectListItem>, new { @class = "form-control" })

... to render everything as expected.
However if I do a submit, and my model is not validated the page is redisplayed, but this time without checked boxes while the list is populated the same way, but for some reason the 'items' parameter has a list without the selected properties set to true.
Another submit reverses this and everything is checked again... If selected is set to false the check boxes are always unchecked as expected. But I cant populate the list with some values set to true that way either (my original problem).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions