合并属性 [英] Merging attributes

查看:136
本文介绍了合并属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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等的Dictionary.我认为MergeAttributes应该在循环中以提取键/值,但不确定是什么开始滚滚滚动?

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?

我希望类具有初始htmlAttributes值"editableInNew editableInUpdate readonly"元素以及添加有.AddCssClass的"checkBoxWithLabel"元素,但无法正常工作,我很沮丧.

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.

推荐答案

您不应尝试在辅助程序中手动生成html,而应使用内置方法.您不仅在编写大量必要的代码,而且没有考虑到HtmlHelper的标准功能,例如绑定到ModelState,客户端验证等(我认为您没有意识到).如果您确实想手动执行此操作,建议您学习

You should not be attempting to manually generate your html in the helper, but rather making use of the built-in methods. Not only are you writing significantly more code that necessary, your not taking into account standard HtmlHelper features such as binding to ModelState, client side validation etc which I assume you are not aware of. If you do want to do this manually, I recommend you study the source code first.

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

You should also change the signature of your helper to allow only boolean properties.

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());
}

这篇关于合并属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆