asp.net mvc: why is Html.CheckBox generating an additional hidden input

后端 未结 11 1871
猫巷女王i
猫巷女王i 2020-11-22 13:59

I just noticed that Html.CheckBox(\"foo\") generates 2 inputs instead of one, anybody knows why is this so ?



        
相关标签:
11条回答
  • 2020-11-22 14:48

    The manual approach is this:

    bool IsDefault = (Request.Form["IsDefault"] != "false");
    
    0 讨论(0)
  • 2020-11-22 14:48

    As of 2020/11 and .NET 5 being in preview, there is a pull request that should make this behavior controllable. Thank you guys!

    Anyway if someone founds it useful, .NET Core 3.0 port of Alexander Trofimov's answer:

    public static IHtmlContent CheckBoxSimple(this IHtmlHelper htmlHelper, string name)
    {
        TextWriter writer = new StringWriter();
    
        IHtmlContent html = htmlHelper.CheckBox(name);
        html.WriteTo(writer, HtmlEncoder.Default);
    
        string checkBoxWithHidden = writer.ToString();
    
        string pureCheckBox = checkBoxWithHidden.Substring(0, checkBoxWithHidden.IndexOf("<input", 1));
        return new HtmlString(pureCheckBox);
    }
    
    0 讨论(0)
  • 2020-11-22 14:52

    I found this really caused issues when I had a WebGrid. The sorting links on the WebGrid would turn by the doubled up querystring or x=true&x=false into x=true,false and cause a parse error in checkbox for.

    I ended up using jQuery to delete the hidden fields on the client side:

        <script type="text/javascript">
        $(function () {
            // delete extra hidden fields created by checkboxes as the grid links mess this up by doubling the querystring parameters
            $("input[type='hidden'][name='x']").remove();
        });
        </script>
    
    0 讨论(0)
  • 2020-11-22 14:53

    You can write a helper to prevent adding the hidden input:

    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    
    public static class HelperUI
    {
        public static MvcHtmlString CheckBoxSimple(this HtmlHelper htmlHelper, string name, object htmlAttributes)
        {
            string checkBoxWithHidden = htmlHelper.CheckBox(name, htmlAttributes).ToHtmlString().Trim();
            string pureCheckBox = checkBoxWithHidden.Substring(0, checkBoxWithHidden.IndexOf("<input", 1));
            return new MvcHtmlString(pureCheckBox);
        }
    }
    

    use it:

    @Html.CheckBoxSimple("foo", new {value = bar.Id})
    
    0 讨论(0)
  • 2020-11-22 15:00

    If checkbox is not selected, form field is not submitted. That is why there is always false value in hidden field. If you leave checkbox unchecked, form will still have value from hidden field. That is how ASP.NET MVC handles checkbox values.

    If you want to confirm that, place a checkbox on form not with Html.Hidden, but with <input type="checkbox" name="MyTestCheckboxValue"></input>. Leave checkbox unchecked, submit form and look at posted request values on server side. You'll see that there is no checkbox value. If you had hidden field, it would contain MyTestCheckboxValue entry with false value.

    0 讨论(0)
提交回复
热议问题