我编写了一个帮助器,它将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))
我想将相同的属性添加到&lt; div&gt;&lt; / div&gt;即没有帮助者。编写一个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() >
答案 0 :(得分:2)
MVC.Net在其Html帮助程序方法内部使用TagBuilder类(MSDN)。
E.g。这是构建Label标签的一些代码:
TagBuilder tag = new TagBuilder("label");
tag.SetInnerText(resolvedLabelText);
tag.MergeAttributes(htmlAttributes, replaceExisting: true);
return tag.ToMvcHtmlString(TagRenderMode.Normal);