0

Model

public class AllControls
{
    public List<Group> getChkItems { get; set; }
    public bool chk { get; set; }
}

public class Group
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Controller:

[HttpGet]
public ActionResult Index()
{       
    List<Group> li = new List<Group>()
    {
        new Group() { ID = 1, Name = "C#" },
        new Group() { ID = 1, Name = "Asp.NET" },
        new Group() { ID = 1, Name = "SQL" }
    };

    AllControls model = new AllControls();
    model.getChkItems = li;
    return View(model);
}

[HttpPost]
public ActionResult Index(AllControls e)
{
    return View(e);
}

View:

@using (Html.BeginForm())
{
    foreach (var x in @Model.getChkItems)
    {    
        @Html.CheckBoxFor(m => m.chk, new { value = @x.ID }) @x.Name
        <br />
    }
    <input type="submit" value="Submit" id="btn" />    
}

How can I get the selected checkbox value and text in the controller?

Mun
  • 14,098
  • 11
  • 59
  • 83
Pearl
  • 8,373
  • 8
  • 40
  • 59
  • 1
    you really don't need value on `Post`, you need only `ID`, if you still need text, you should put it in `hidden` hield (`Html.Hidden` helper). I suppose you still have problems with model binding on `Post`, [here](http://stackoverflow.com/questions/5284395/checkboxlist-in-mvc3-view-and-get-the-checked-items-passed-to-the-controller) is a good working example how you can avoid binding problem. – teo van kot Jul 07 '15 at 06:02

1 Answers1

9

Here goes my solution. Let your model be as shown below.

public class CheckboxModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
}

public class MainModel
{
    public List<CheckboxModel> CheckBoxes { get; set; }
}

And let your Controller GET Action be as shown below.

public ActionResult GetDatas()
{
    MainModel model = new MainModel();
    var list = new List<CheckboxModel>
    {
         new CheckboxModel{Id = 1, Name = "India", Checked = false},
         new CheckboxModel{Id = 2, Name = "US", Checked = false},
         new CheckboxModel{Id = 3, Name = "UK", Checked = false}

    };
    model.CheckBoxes = list;
    return View(model);
}

And POST Action be as shown below.

[HttpPost]
public ActionResult PostDatas(MainModel model)
{
    return View(model);
}

The View should be as shown below.

@model WebApplication1.Controllers.MainModel
@using (Html.BeginForm("PostDatas","Home"))
{
    for (var i = 0; i < Model.CheckBoxes.Count; i++)
    {
        <table>
            <tr>
                <td>
                    @Html.HiddenFor(m => Model.CheckBoxes[i].Id)
                    @Html.HiddenFor(m => Model.CheckBoxes[i].Name)
                    @Html.CheckBoxFor(m => Model.CheckBoxes[i].Checked)
                </td>
                <td>
                    @Html.DisplayFor(m => Model.CheckBoxes[i].Name)                    
                </td>
            </tr>
        </table>

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

View will be rendered as shown below.

enter image description here

When you select India and US and click on submit button, you will get POST parameters as below.

enter image description here

ramiramilu
  • 17,044
  • 6
  • 49
  • 66