如何在自定义帮助器中合并htmlAttributes [英] How to merge htmlAttributes in Custom Helper
问题描述
我有一个自定义帮助器,在其中我收到了htmlAttributes作为参数:
public static MvcHtmlString Campo<TModel, TValue>(
this HtmlHelper<TModel> helper,
Expression<Func<TModel, TValue>> expression,
dynamic htmlAttributes = null)
{
var attr = MergeAnonymous(new { @class = "form-control"}, htmlAttributes);
var editor = helper.EditorFor(expression, new { htmlAttributes = attr });
...
}
MergeAnonymous方法必须返回在参数中接收到的带有"new {@class ="form-control"}的合并的htmlAttributes:
static dynamic MergeAnonymous(dynamic obj1, dynamic obj2)
{
var dict1 = new RouteValueDictionary(obj1);
if (obj2 != null)
{
var dict2 = new RouteValueDictionary(obj2);
foreach (var pair in dict2)
{
dict1[pair.Key] = pair.Value;
}
}
return dict1;
}
在示例字段的编辑器模板中,我需要添加更多属性:
@model decimal?
@{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
htmlAttributes["class"] += " inputmask-decimal";
}
@Html.TextBox("", string.Format("{0:c}", Model.ToString()), htmlAttributes)
我在编辑器模板的最后一行的htmlAttributes中具有的内容是:
点击此处查看图片 >
请注意,类"显示正确,但是扩展助手中的其他属性在字典中,我在做什么错了?
如果可能的话,我只想更改扩展助手而不是编辑器模板,所以我认为传递给EditorFor的RouteValueDictionary需要转换为匿名对象...
我解决了现在将所有编辑器模板"更改为以下行的问题:
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
为此:
var htmlAttributes = ViewData["htmlAttributes"] as IDictionary<string, object> ?? HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
以及对此的MergeAnonymous方法:
static IDictionary<string,object> MergeAnonymous(object obj1, object obj2)
{
var dict1 = new RouteValueDictionary(obj1);
var dict2 = new RouteValueDictionary(obj2);
IDictionary<string, object> result = new Dictionary<string, object>();
foreach (var pair in dict1.Concat(dict2))
{
result.Add(pair);
}
return result;
}
I have a Custom Helper where I receive a htmlAttributes as parameter:
public static MvcHtmlString Campo<TModel, TValue>(
this HtmlHelper<TModel> helper,
Expression<Func<TModel, TValue>> expression,
dynamic htmlAttributes = null)
{
var attr = MergeAnonymous(new { @class = "form-control"}, htmlAttributes);
var editor = helper.EditorFor(expression, new { htmlAttributes = attr });
...
}
The MergeAnonymous method must return the merged htmlAttributes received in parameter with "new { @class = "form-control"}":
static dynamic MergeAnonymous(dynamic obj1, dynamic obj2)
{
var dict1 = new RouteValueDictionary(obj1);
if (obj2 != null)
{
var dict2 = new RouteValueDictionary(obj2);
foreach (var pair in dict2)
{
dict1[pair.Key] = pair.Value;
}
}
return dict1;
}
And in the Editor Template for an example field I need to add some more attributes:
@model decimal?
@{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
htmlAttributes["class"] += " inputmask-decimal";
}
@Html.TextBox("", string.Format("{0:c}", Model.ToString()), htmlAttributes)
What I have in htmlAttributes at last line in Editor Template is:
Note that the "class" is appearing correctly, but the others attributes from the Extension Helper are in a Dictionary, what am I doing wrong?
If possible, I want to change only Extension Helper and not Editor Template, so I think the RouteValueDictionary passed to EditorFor need to cast to a anonymous object...
I solved for now changing all my Editor Templates the line:
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
for this:
var htmlAttributes = ViewData["htmlAttributes"] as IDictionary<string, object> ?? HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
and the MergeAnonymous method to this:
static IDictionary<string,object> MergeAnonymous(object obj1, object obj2)
{
var dict1 = new RouteValueDictionary(obj1);
var dict2 = new RouteValueDictionary(obj2);
IDictionary<string, object> result = new Dictionary<string, object>();
foreach (var pair in dict1.Concat(dict2))
{
result.Add(pair);
}
return result;
}
这篇关于如何在自定义帮助器中合并htmlAttributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!