我有一个自定义助手:如何,我收到htmlAttributes作为参数合并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方法必须返回合并htmlAttributes参数接收到“新{@class =”形式 - 控制“}”:
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是:
注意,“类”是正确显示,但其他人的扩展帮助属性是一个字典,什么是我做错了?
如果可能的话,我想只改变扩展帮助,而不是编辑模板,所以我觉得RouteValueDictionary传给EditorFor需要转换到一个匿名对象...
https://cpratt.co/html-editorfor-and-htmlattributes/ – 2017-05-13 05:13:53