I am trying to translate this razor code to C# code. However, having problem with checkbox postbacks.
<div class="input-group">
@Html.TextBoxFor(model => model.Property1, new { @class = "form-control js-template-text", data_val = "false" }
<span class="input-group-addon">
@Html.CheckBoxFor(model => model.Property2, new { data_val = "false" })
@Html.HiddenFor(model => model.Property2)
<span class="glyphicon glyphicon-phone glyphicon-green"></span>
</span>
</div>
This bit of the markup works just fine. But the markup generated from this c# code does not. The generated markup from both approaches are the same. My question is: what modification (if any at all) is needed in the code, so that browser should send the correct checkbox value.
private static MvcHtmlString CreateTextBoxCheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, string>> textBoxExpression, Expression<Func<TModel, bool>> checkBoxExpression, IGlyphIcon checkBoxGlyphIcon = null, IDictionary<string, object> htmlAttributes = null, IDictionary<string, object> textBoxHtmlAttributes = null, IDictionary<string, object> checkBoxHtmlAttributes = null)
{
var sb = new StringBuilder();
#region OuterDiv
var outerDivTag = new TagBuilder("div");
outerDivTag.MergeAttributes(htmlAttributes);
outerDivTag.AddCssClass("input-group");
sb.AppendLine(outerDivTag.ToString(TagRenderMode.StartTag));
#endregion OuterDiv
#region TextBox
var textBoxName = ExpressionHelper.GetExpressionText(textBoxExpression);
var textBoxFullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(textBoxName);
var textBoxId = TagBuilder.CreateSanitizedId(textBoxFullName);
var textBoxMetaData = ModelMetadata.FromLambdaExpression(textBoxExpression, htmlHelper.ViewData);
var textBoxValue = textBoxMetaData.Model.ToString();
var textBoxTag = new TagBuilder("input");
textBoxTag.Attributes.Add("type", "text");
textBoxTag.Attributes.Add("class", "form-control");
textBoxTag.Attributes.Add("name", textBoxFullName);
textBoxTag.Attributes.Add("id", textBoxId);
textBoxTag.Attributes.Add("value", textBoxValue);
// get data annotation/client side scripts attributes
var textkBoxValidationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(textBoxFullName, textBoxMetaData);
foreach (var key in textkBoxValidationAttributes.Keys)
{
textBoxTag.Attributes.Add(key, textkBoxValidationAttributes[key].ToString());
}
textBoxTag.MergeAttributes(textBoxHtmlAttributes, true);
sb.AppendLine(textBoxTag.ToString(TagRenderMode.SelfClosing));
#endregion TextBox
#region CheckBox
var checkBoxSpan = new TagBuilder("span");
checkBoxSpan.Attributes.Add("class", "input-group-addon");
var checkBoxName = ExpressionHelper.GetExpressionText(checkBoxExpression);
var checkBoxFullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(checkBoxName);
var checkBoxId = TagBuilder.CreateSanitizedId(checkBoxFullName);
var checkBoxMetaData = ModelMetadata.FromLambdaExpression(checkBoxExpression, htmlHelper.ViewData);
var checkBoxValue = checkBoxMetaData.Model.ToString().ToLower();
var checkBoxTag = new TagBuilder("input");
checkBoxTag.Attributes.Add("type", "checkbox");
checkBoxTag.Attributes.Add("name", checkBoxFullName);
checkBoxTag.Attributes.Add("id", checkBoxId);
checkBoxTag.Attributes.Add("value", checkBoxValue);
if (checkBoxValue.Equals("true", StringComparison.InvariantCultureIgnoreCase))
{
checkBoxTag.Attributes.Add("checked", "checked");
}
// get data annotation/client side scripts attributes
var validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(checkBoxFullName, checkBoxMetaData);
foreach (var key in validationAttributes.Keys)
{
checkBoxTag.Attributes.Add(key, validationAttributes[key].ToString());
}
checkBoxTag.MergeAttributes(checkBoxHtmlAttributes, true);
// to keep track of checkbox postbacks, create hidden input for checkbox
var checkBoxHiddenTag = new TagBuilder("input");
checkBoxHiddenTag.Attributes.Add("name", checkBoxFullName);
checkBoxHiddenTag.Attributes.Add("id", checkBoxId);
checkBoxHiddenTag.Attributes.Add("type", "hidden");
checkBoxHiddenTag.Attributes.Add("value", checkBoxValue.Equals("true", StringComparison.InvariantCultureIgnoreCase) ? "True" : "False");
sb.AppendLine(checkBoxSpan.ToString(TagRenderMode.StartTag));
sb.AppendLine(checkBoxTag.ToString(TagRenderMode.SelfClosing));
sb.AppendLine(checkBoxHiddenTag.ToString(TagRenderMode.SelfClosing));
#endregion CheckBox
#region GlyphIcon
if (checkBoxGlyphIcon != null)
{
var checkBoxGlyphIconSpan = new TagBuilder("span");
checkBoxGlyphIconSpan.Attributes.Add("class", checkBoxGlyphIcon.CssClass);
sb.AppendLine(checkBoxGlyphIconSpan.ToString(TagRenderMode.StartTag));
sb.AppendLine(checkBoxGlyphIconSpan.ToString(TagRenderMode.EndTag));
}
#endregion GlyphIcon
sb.AppendLine(checkBoxSpan.ToString(TagRenderMode.EndTag));
sb.AppendLine(outerDivTag.ToString(TagRenderMode.EndTag));
var result = MvcHtmlString.Create(sb.ToString());
return result;
}