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

后端 未结 11 1872
猫巷女王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

    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("

提交回复
热议问题