Search code examples
asp.net-mvccheckboxlist

MVC Controllers get value of 'Checkboxlist'


I'm probably a idiot here but I'm having problems getting the value of whether or not a checkbox is checked/selected or not. Here's what I've got so far:

In my Model:

public IEnumerable<SelectListItem> Insurers
{
  get
    { 
      var list = new List<SelectListItem>();
      string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton";
      string[] zInsurers = zInsurersList.Split(',');
      foreach (string aInsurer in zInsurers)
      {
        list.Add(new SelectListItem() { Text = aInsurer, Value = aInsurer, Selected=false});
      }
      return list;
    }
  }
}

And my view:

@foreach (var insurer in @Model.Insurers)
{
  var zInsurer = insurer.Text;
  var zValue = insurer.Value;
  <tr>
    <td style="width: 120px; height: 35px;"><span id="@zInsurer">@zInsurer</span></td>
    <td style="width: 40px; height: 35px;"><input id="@zInsurer" type="checkbox" name="@zInsurer"></td>
  </tr>
}

So in my controller I'm trying to loop the list and get the value of whether or not the user has selected the option:

foreach (var item in model.Insurers)
{
  //if (item.GetType() == typeof(CheckBox))
  //string controlVal = ((SelectListItem)item).Selected.ToString();
  zInsurers = zInsurers + item.Text + " " + ((SelectListItem)item).Selected.ToString() + "<br/>";
}

But the value always returns false.

Could someone spare a few mins to highlight my stupidity please?

Thanks,

Craig


Solution

  • There are a lot of ways to do it. I normally add String Array in model to collect selected values.

    public string[] SelectedInsurers { get; set; }
    
    <input type="checkbox" name="SelectedInsurers" value="@insurer.Value" />
    

    Here is the sample code -

    MyModel

    public class MyModel
    {
        public string[] SelectedInsurers { get; set; }
    
        public IEnumerable<SelectListItem> Insurers
        {
            get
            {
                var list = new List<SelectListItem>();
                string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton";
                string[] zInsurers = zInsurersList.Split(',');
                foreach (string aInsurer in zInsurers)
                {
                    list.Add(new SelectListItem { Text = aInsurer, Value = aInsurer, Selected = false });
                }
    
                return list;
            }
        }
    }
    

    Action Methods

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

    View

    @using (Html.BeginForm())
    {
        foreach (var insurer in @Model.Insurers)
        {
            <input type="checkbox" name="SelectedInsurers" value="@insurer.Value" /> @insurer.Text<br/>
        }
        <input type="submit" value="Post Back" />
    }
    

    Result

    enter image description here