-1

HTML Helper class

@Html.CheckBox("Checkbox1", false)
@Html.CheckBox("Checkbox1", false)
@Html.CheckBox("Checkbox1", true)

Controller:

    [HttpPost]
    public ActionResult Index(string[] Checkbox1)
    {
        return View();
    }

How to get the selected checkbox values in Controller? Here I'm getting all the checkbox values along with the selected values. Suppose, if I have selected 2 checkboxes, I'm getting 3+2 = 5 values. Please HELP.

Billy
  • 825
  • 6
  • 21
  • 36
  • possible duplicate of [How to handle checkboxes in ASP.NET MVC forms?](http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-asp-net-mvc-forms) – Shaun Luttin Jul 03 '15 at 18:59

2 Answers2

1

The @Html.CheckBox() and @Html.CheckBoxFor() helper methods generate 2 inputs, a checkbox and a hidden input

<input type="checkbox" name="yourPropertyName" .... value="true" >
<input type="hidden" name="yourPropertyName" value="false" >

This is by design because unchecked checkboxes to not post a value. If checked, then yourPropertyName=true&yourPropertyName=false is submitted. The DefaultModelBinder reads the first value (true) and ignores the second (so the property is set to true). If unchecked then just yourPropertyName=false is submitted and the property is set to false.

It hard to understand what you trying to achieve by creating 3 inputs with the same name, and the fact your claiming your getting 5 values means that your using FormCollection to read the values (2 of the checkboxes are checked and one is not). You should NEVER use FormCollection!

Use a view model and strongly typed html helpers to generate your views, and post back the model. For example

public class MyModel
{
  public bool Property1 { get; set; }
  public bool Property2 { get; set; }
  ....
}

and in the view

@model MyModel
....
@Html.CheckBoxFor(m => m.Property1)
@Html.CheckBoxFor(m => m.Property2)
....

Or if your wanting a collection of boolean properties

public class MyModel
{
  public List<bool> MyProperty { get; set; }
  ....
}

and in the view

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

Then in the controller

public ActionResult Edit(MyModel model)
{
  // model will be correctly bound with the values of the boolean properties
}
0

You'll need to have distinct values for your check boxes. Can you provide more context to the use of this collection of check boxes?

Perhaps you could name them each something unique and do something like the following?

string checkboxValue1 = Request["Checkbox1"].ToString();
string checkboxValue2 = Request["Checkbox2"].ToString();
string checkboxValue3 = Request["Checkbox3"].ToString();

bool checkboxStatus1 = false;
bool checkboxStatus2 = false;
bool checkboxStatus3 = false;


if (checkboxValue1 == "true,false")
    checkboxStatus1 = true; 

if (checkboxValue2 == "true,false")
    checkboxStatus2 = true; 

if (checkboxValue3 == "true,false")
    checkboxStatus3 = true; 
Ron Brogan
  • 892
  • 1
  • 9
  • 25