3

This is my view page Markup

<%for (int i = 0; i < 3; i++)
  { %>


  <%:Html.CheckBox("Test") %>

<%} %>

and this is my controller where i am getting the values from formcollection object

public ActionResult CreateTaxMaster(TaxMaster objTaxMaster ,bool [] Test,FormCollection form)
{ 
    string LocalCheckboxValues = string.Empty;
    foreach (var key in form.AllKeys)
    {
         if (key.Contains("Test"))
         {
              LocalCheckboxValues = LocalCheckboxValues + form.Get(key);                 
         }

}

i tried all the three way to get the proper values but its giving me some true,false values with mismatch selected values of checkbox

if i select all three checkbox still it is giving the formcollection values in true,false,true,false,true,false

Any help woulde be appriciated. Thanks in advance.

Nikhil waghmare
  • 31
  • 1
  • 1
  • 5

5 Answers5

6

Its because when the check box is selected the values passed are "true,false"

Using Contains("true"); You can check whether the checkbox is selected or not

For eg:

bool bChecked = form[key].Contains("true");

Jatin patil
  • 4,252
  • 1
  • 18
  • 27
0

see what happening after the checkbox is checked the the hidden field get generated to store the false values which used to send it back to the server , then i go with following sequence to tack this situation,i have removed the next element of of form collection object which is the value of hidden field. like as follows

if(Convert.ToBoolean(form["Test"])== true){form["Test"].RemoveAt(i+1)}
Satpal
  • 132,252
  • 13
  • 159
  • 168
Nikhil waghmare
  • 31
  • 1
  • 1
  • 5
0

Without the hidden, only the checked values would be posted. If you use bool[] as a parameter you get the checkbox and the hidden value. so false gives you one entry and true gives you a matching false.

Why does the CheckBoxFor render an additional input tag, and how can I get the value using the FormCollection?

    /// <summary>
    /// If you posted an array of checkboxes to a controller, this will extract the values you expect.
    /// </summary>
    /// <param name="arrayOfCheckboxesFromController">An array of checkboxes passed to the controller</param>        
    /// <remarks>with checkboxes, true values come with a twin false so remove it</remarks>
    private static void GetCheckboxArrayValues(IList<bool> arrayOfCheckboxesFromController)
    {
        for (var i = 0; i < arrayOfCheckboxesFromController.Count(); i++)
        {
            if (!arrayOfCheckboxesFromController[i]) continue;

            // This assumes the caller knows what they are doing and passed in an array of checkboxes posted to a controller
            arrayOfCheckboxesFromController.RemoveAt(i + 1);
        }
    }
Community
  • 1
  • 1
Jacob Brewer
  • 2,574
  • 1
  • 22
  • 25
0
List<bool> bools = Request["Test"].Split(',').Select(n => string.Compare(n, "true", true) == 0? true : false).ToList();

for (int i = 0; i < bools.Count(); ++i)
{
    if (bools[i]) bools.RemoveAt(i + 1);
}
George
  • 1
  • 1
    Only posting code does not make a good answer. Add some explanation as to why this is the correct code, so others can understand the underlying logic, avoid similar issues, and tweak the code to fit their own needs. – CodeMouse92 Sep 24 '15 at 19:42
0

MVC renders a hidden checkbox with the same ID, so we get both values. The visible one is first, so you can use the comma and split to get it like this (also works if only one value):

bool ActualValue = Convert.ToBoolean(collection["BoolFieldName"].ToString().Split(',')[0]);
SteveCav
  • 6,649
  • 1
  • 50
  • 52