合并属性

时间:2015-12-15 17:33:25

标签: c# html asp.net-mvc html-helper

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)仅替换&#34; _&#34;与&#34; - &#34;。虽然MergeAttributes期望键/值类型,因此忽略现有值。无法使用IEnumerable,IDictionary等将对象HtmlAttributes更改/转换为字典。我认为MergeAttributes应该在循环中提取键/值但不确定是什么开始滚动?

我希望类具有初始的htmlAttributes值&#34; editableInNew editableInUpdate readonly&#34;元素与&#34; checkBoxWithLabel&#34;添加了.AddCssClass,但无法让它工作,我很难过。

1 个答案:

答案 0 :(得分:1)

您不应该尝试在帮助程序中手动生成html,而是使用内置方法。您不仅要编写更多必要的代码,而且还要考虑标准的HtmlHelper功能,例如绑定到ModelState,客户端验证等我认为您不知道的。如果您确实想手动执行此操作,我建议您先学习source code

您还应该更改助手的签名,以仅允许boolean属性。

public static IHtmlString CheckBoxWithLabelFor<TModel>(this HtmlHelper<TModel> helper,
    Expression<Func<TModel, bool>> expression, string labelText, object htmlAttributes)
{
    IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    // add the "checkBoxWithLabel" class
    if (attributes.ContainsKey("class"))
    {
        attributes["class"] = "checkBoxWithLabel " + attributes["class"];
    }
    else
    {
        attributes.Add("class", "checkBoxWithLabel");
    }
    // build the html
    StringBuilder html = new StringBuilder();
    html.Append(helper.CheckBoxFor(expression, attributes));
    html.Append(helper.LabelFor(expression, labelText, new { @class = "checkBoxLabel" }));
    // suggest also adding the validation message placeholder
    html.Append(helper.ValidationMessageFor(expression));
    return MvcHtmlString.Create(html.ToString());
}