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

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

    This is the strongly typed version of Alexander Trofimov's solution:

    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    
    public static class HelperUI
    {
        public static MvcHtmlString CheckBoxSimpleFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes)
        {
            string checkBoxWithHidden = htmlHelper.CheckBoxFor(expression, htmlAttributes).ToHtmlString().Trim();
            string pureCheckBox = checkBoxWithHidden.Substring(0, checkBoxWithHidden.IndexOf("

提交回复
热议问题