0

我编写了一个帮助器,它将 html 属性作为字典返回,然后任何帮助器都使用它来呈现属性。

这些属性然后被一些 javascript 代码使用。

代码不重要,但在这里

namespace MvcHtmlHelpers
{
    public class PropertyValuePairs<TModel>
    {
        public readonly IDictionary<string, object> Pairs = new Dictionary<string, object>();
        private HtmlHelper<TModel> _html;
        public PropertyValuePairs(HtmlHelper<TModel> html)
        {
            _html=html;
        }
        public PropertyValuePairs<TModel> AddNameFor<TValue>(Expression<Func<TModel, TValue>> expression, object value)
        {
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string name = _html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
            return AddName(name, value);
        }
        public PropertyValuePairs<TModel> AddName(string name, object value)
        {
            Pairs.Add(name, value);
            return this;
        }
        public PropertyValuePairs<TModel> AddIdFor<TValue>(Expression<Func<TModel, TValue>> expression, object value)
        {
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string id = _html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName);
            return AddName(id, value);
        }
        public PropertyValuePairs<TModel> AddId(string id, object value)
        {
            Pairs.Add('#' + id, value);
            return this;
        }
    }
    public enum LogicalOperator { And, Or }
    public static class EnabledIfAttributes
    {
        public static PropertyValuePairs<TModel> NewPropertyValues<TModel>(this HtmlHelper<TModel> html)
        {
            return new PropertyValuePairs<TModel>(html);
        }
        //for use in javaScript - lower case property names are intentional
        public class PropertyPair
        {
            public string name { get; set; }
            public object val { get; set; }
        }
        public class dataAttribute
        {
            public string logicalOperator { get; set; }
            public List<PropertyPair> propertyPairs { get; set; }
        }
        public const string ClassName = "enabledif";
        public static string AttributeName = "data-" + ClassName;
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object value)
        {
            return EnabledIfAttributesFor(html, expression, value, new RouteValueDictionary());
        }
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object value, object htmlAttributes)
        {
            return EnabledIfAttributesFor(html, expression, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object value, IDictionary<string, object> htmlAttributes)
        {
            var pairList = new PropertyValuePairs<TModel>(html).AddNameFor(expression, value);
            return EnabledIfAttributesFor(html, pairList, new RouteValueDictionary(htmlAttributes));
        }
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel>(this HtmlHelper<TModel> html, PropertyValuePairs<TModel> values, LogicalOperator logicalOperator = LogicalOperator.Or)
        {
            return EnabledIfAttributesFor(html, values, new RouteValueDictionary(), logicalOperator);
        }
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel>(this HtmlHelper<TModel> html, PropertyValuePairs<TModel> values, object htmlAttributes, LogicalOperator logicalOperator = LogicalOperator.Or)
        {
            return EnabledIfAttributesFor(html, values, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), logicalOperator);
        }
        public static IDictionary<string, object> EnabledIfAttributesFor<TModel>(this HtmlHelper<TModel> html, PropertyValuePairs<TModel> values, IDictionary<string, object> htmlAttributes, LogicalOperator logicalOperator = LogicalOperator.Or)
        {
            //in this case expression refers to other property 
            if (htmlAttributes.ContainsKey("class"))
            {
                string existingClass = htmlAttributes["class"].ToString().Trim();
                htmlAttributes["class"] = existingClass + ' ' + ClassName; 
            }
            else
            {
                htmlAttributes.Add("class",ClassName);
            }

            var attr = new dataAttribute
            {
                logicalOperator = logicalOperator.ToString(),
                propertyPairs = new List<PropertyPair>()
            };

            foreach (var pair in values.Pairs)
            {
                string htmlFieldName = ExpressionHelper.GetExpressionText(pair.Key);
                attr.propertyPairs.Add(new PropertyPair
                {
                    name = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName),
                    val = pair.Value
                });
            }

            htmlAttributes.Add(AttributeName, JsonConvert.SerializeObject(attr));
            return htmlAttributes;
        }

    }
}

它像这样在代码中使用

@Html.CheckBoxFor(model => model.AllExclusionCriteriaAbsent, @Html.EnabledIfAttributesFor(model=>model.AllInclusionCriteriaPresent, true))

我想将相同的属性添加到 <div></div> 即没有助手。编写一个 div 助手是愚蠢的,所以我想知道是否有一种预先存在的方法可以将字典呈现为 html 属性(MVC 附带的所有助手都必须使用一个),或者一种干净/简洁的方法这与剃须刀 2。谢谢。

编辑

使用罗斯的建议,似乎有两种更简单的方法可以解决这个问题:

namespace MvcHtmlHelpers
{
    public static class SimpleTagHelper
    {
        public static MvcHtmlString Element(string tagName, IDictionary<string, object> attributes, TagRenderMode renderMode=TagRenderMode.Normal)
        {
            var tag = new TagBuilder(tagName);
            tag.MergeAttributes(attributes);
            return MvcHtmlString.Create(tag.ToString(renderMode));
        }
        public static MvcHtmlString ToAttributes(this IDictionary<string, object> attributeDictionary)
        {
            return MvcHtmlString.Create(string.Join(" ",attributeDictionary.Select(d=>string.Format("{0}=\"{1}\"",d.Key,HttpUtility.HtmlAttributeEncode(d.Value.ToString())))));
        }
    }
}

然后选择您喜欢的语义:

@SimpleTagHelper.Element("div", Html.EnabledIfAttributesFor(model => model.AllExclusionCriteriaAbsent, true, new { @class="optionList"}), TagRenderMode.StartTag)

或者

<div @Html.EnabledIfAttributesFor(model => model.AllExclusionCriteriaAbsent, true, new { @class="optionList"}).ToAttributes() >
4

1 回答 1

2

MVC.Net在其 Html 辅助方法内部使用 TagBuilder 类 ( MSDN )。

例如,这里有一些用于构建标签标签的代码:

TagBuilder tag = new TagBuilder("label");
tag.SetInnerText(resolvedLabelText);
tag.MergeAttributes(htmlAttributes, replaceExisting: true);
return tag.ToMvcHtmlString(TagRenderMode.Normal);
于 2013-06-04T09:07:49.500 回答