合并属性

本文关键字:属性 合并 | 更新日期: 2023-09-27 18:26:51

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(仅将"_"替换为"-"。 而 MergeAttributes 需要键/值类型,因此忽略现有值。 无法更改/强制转换对象 HtmlAttributes 到具有 IEnumerable、IDictionary 等的字典。 我认为 MergeAttributes 应该在循环中提取键/值,但不确定是什么开始滚动?

我希望类具有初始 htmlAttributes 值"editableInNew editableInUpdate readonly"元素以及添加的"checkBoxWithLabel"。AddCss类,但无法让它工作,我被难住了。

合并属性

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

还应更改帮助程序的签名以仅允许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());
}