简体   繁体   English

Html.checkbox 控件表单集合值带有一些额外的真假值

[英]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这是我的控制器,我从 formcollection 对象中获取值

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如果我选择了所有三个复选框,它仍然会在 true、false、true、false、true、false 中给出 formcollection 值

Any help woulde be appriciated.任何帮助将被应用。 Thanks in advance.提前致谢。

Its because when the check box is selected the values passed are "true,false"它因为选中复选框时,传递的值是“true,false”

Using Contains("true");使用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.如果您使用 bool[] 作为参数,您将获得复选框和隐藏值。 so false gives you one entry and true gives you a matching false.所以 false 给你一个条目,true 给你一个匹配的 false。

Why does the CheckBoxFor render an additional input tag, and how can I get the value using the FormCollection? 为什么 CheckBoxFor 会呈现额外的输入标签,以及如何使用 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. MVC 呈现一个具有相同 ID 的隐藏复选框,因此我们获得了两个值。 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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