jeudi 21 avril 2016

CheckBoxFor mvc5 not bidding to form submit

I override the code of checkbox to reproduze a checkbox more label, but when I submit action, the value of checkbox won't be submit.

My code is:

 private static MvcHtmlString AuxCheckBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            if (String.IsNullOrEmpty(labelText))
            {
                return MvcHtmlString.Empty;
            }

            TagBuilder tagCheckBox = new TagBuilder("input");
            tagCheckBox.MergeAttributes(htmlAttributes);
            tagCheckBox.Attributes.Add("class", "checkbox-custom");
            tagCheckBox.Attributes.Add("id", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
            tagCheckBox.Attributes.Add("type", "checkbox");

            TagBuilder tagLabel = new TagBuilder("label");
            tagLabel.Attributes.Add("class", "checkbox-custom-label");
            tagLabel.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
            tagLabel.SetInnerText(labelText);

            StringBuilder sb = new StringBuilder();
            sb.Append(tagCheckBox.ToString(TagRenderMode.Normal));
            sb.Append(tagLabel.ToString(TagRenderMode.Normal));

            return MvcHtmlString.Create(sb.ToString());
        }




Aucun commentaire:

Enregistrer un commentaire