Fabrik.Common icon indicating copy to clipboard operation
Fabrik.Common copied to clipboard

CheckBoxListFor posted values weirdness

Open Jogai opened this issue 9 years ago • 0 comments

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 https://github.com/benfoster/Fabrik.Common/issues/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).

Jogai avatar Mar 31 '15 14:03 Jogai