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?

1
  • 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 is a good working example how you can avoid binding problem. Commented Jul 7, 2015 at 6:02

1 Answer 1

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

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

5 Comments

Thanks a lot. But we really need to take Hidden Fields?
To pass Name and ID, yes we need them.
Here we are passing the data from controller to view as list type. How to use Class as model in the same type?
Updated my answer, please check.
I am not getting MainModel in WebApplication1.Controllers.MainModel

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.