简体   繁体   中英

Html.checkbox control form collection values are getting with some extra true false values

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.

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");

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)}

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);
        }
    }
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);
}

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM