public static IHtmlString CheckBoxWithLabelFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression, string labelText, object htmlAttributes)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
object currentValue = metadata.Model;
string property = ExpressionHelper.GetExpressionText(expression);
var checkBox = new TagBuilder("input");
checkBox.AddCssClass("checkBoxWithLabel");
checkBox.GenerateId(property);
checkBox.Attributes["type"] = "checkbox";
checkBox.Attributes["name"] = property;
checkBox.Attributes["value"] = "true";
checkBox.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),false);/*added false*/
var hidden = new TagBuilder("input");
hidden.Attributes["type"] = "hidden";
hidden.Attributes["name"] = property;
hidden.Attributes["value"] = "false";
if (Equals(currentValue, true))
{
checkBox.Attributes["checked"] = "checked";
}
var label = new TagBuilder("label");
label.AddCssClass("checkBoxLabel");
var htmlText = label.ToString().Replace("</label>", "");
htmlText += checkBox.ToString(TagRenderMode.SelfClosing);
htmlText += hidden.ToString(TagRenderMode.SelfClosing);
htmlText += labelText + "</label>";
return new HtmlString(htmlText);
AnonymousObjectToHtmlAttributes(htmlAttributes) only replaces "_" with "-". Whilst MergeAttributes expects a key/value type and is therefore ignoring the existing values. Cant change/cast the object HtmlAttributes to a Dictionary with IEnumerable, IDictionary etc. I think MergeAttributes should be in a loop to extract the key/values but not sure what starts the ball rolling?
I want class to have the initial htmlAttributes value "editableInNew editableInUpdate readonly" elements together with the "checkBoxWithLabel" added with .AddCssClass but cant get it work and I'm stumped.