1

I'm trying to create a simple scrollable list of checkboxes in ASP.NET MVC 5. I got a single checkbox to work and it correctly posts back the right value to model.Checked:

Controller

using System.Web.Mvc;
using Checkbox1.Models;

namespace Checkbox1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new TestModel
            {
                Checked = true
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(TestModel model)
        {
            return View(model);
        }
    }
}

View

@model Checkbox1.Models.TestModel


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

    @Html.CheckBoxFor(m => m.Checked, Model.Checked)

    <input type="submit" value="submit" />

}

Model

namespace Checkbox1.Models
{

    public class TestModel
    {
        public bool Checked { get; set; }

    }

}

But when I try the same thing with a list of bools I can't get the bool value to pass back into the Controller. It looks like the model is getting re-instantiated and nulled out when it gets posted back to the Controller:

Controller

using System.Collections.Generic;
using System.Web.Mvc;
using Checkbox2.Models;

namespace Checkbox2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new TestModel()
            {
                lstChecked = new List<bool>()
            };

            model.lstChecked.Add(true);

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(TestModel model)
        {
            return View(model);
        }


    }
}

View

@model Checkbox2.Models.TestModel


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

    @Html.CheckBoxFor(m => m.lstChecked[0], Model.lstChecked[0])

    <input type="submit" value="submit" />

}

Model

using System.Collections.Generic;
using System.Web;

namespace Checkbox2.Models
{
    public class TestModel
    {
        public List<bool> lstChecked;

    }

}

I'd like to know why this is not working but if you could point me to a simple working example of a scrollable list of checkboxes that would also be greatly appreciated!

4
  • Will Html.CheckListBoxFor work? Commented Jan 6, 2020 at 19:49
  • I don't see a Html.CheckListBoxFor object. Commented Jan 6, 2020 at 20:05
  • Doh! You are correct, There is no CheckListBoxFor 0_O Commented Jan 6, 2020 at 20:12
  • You would think they would have one. Or maybe an option in the ListBoxFor to display each item with a checkbox. I'm about ready to just use the ListBoxFor since I have that working. I just thought the CheckBox would be better than having to hold the control key for multiple selections. Commented Jan 6, 2020 at 22:05

1 Answer 1

1

To render a list of checkboxes that correspond to the bools in your list, you need to loop over the list and add a checkbox with the index of each item.

foreach(int i = 0; i <+ Model.lstChecked.Count; i++){
    @Html.CheckBoxFor(m => m.lstChecked[i])
}

Also, what is the purpose of the second parameter in @Html.CheckBoxFor(m => m.lstChecked[0], Model.lstChecked[0])? The first one should take care of populating the checkbox with the value from the model and binding the user's input for the controller on submit.

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

3 Comments

The first example worked with one parameter, but I'm still having the same problem with the list (the second example). It's still not passing back the selected value
Just to clarify it's still not binding the data to the user's input when I use a list.
Perhaps because the property is missing a get and set?

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.