Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EditorFor在MVC5中添加动态HTML属性?_C#_Asp.net Mvc 5.1 - Fatal编程技术网

C# 如何使用EditorFor在MVC5中添加动态HTML属性?

C# 如何使用EditorFor在MVC5中添加动态HTML属性?,c#,asp.net-mvc-5.1,C#,Asp.net Mvc 5.1,我一直在为MVC5站点制作一个扩展方法。其基本思想是,它将为我们的输入组创建标记(即,放入标签、输入和验证消息)。这些组将有一些始终存在的标准HTML和CSS类。到目前为止,这是我所拥有的,它工作得很好 public static MvcHtmlString CreateEditorForGroup<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TP

我一直在为MVC5站点制作一个扩展方法。其基本思想是,它将为我们的输入组创建标记(即,放入标签、输入和验证消息)。这些组将有一些始终存在的标准HTML和CSS类。到目前为止,这是我所拥有的,它工作得很好

public static MvcHtmlString CreateEditorForGroup<TModel, TProperty>(this HtmlHelper<TModel> helper,
    Expression<Func<TModel, TProperty>> expression)
{
    TagBuilder editorLabel = new TagBuilder("span");
    editorLabel.AddCssClass("form-label");
    editorLabel.InnerHtml += helper.LabelFor(expression);

    TagBuilder wrappingDiv = new TagBuilder("div");
    wrappingDiv.InnerHtml = editorLabel.ToString() + helper.EditorFor(expression, new { htmlAttributes = new { @class = "form-control" }}).ToString() + helper.ValidationMessageFor(expression).ToString();
    return new MvcHtmlString(wrappingDiv.ToString());
}
并获得预期结果(在我的例子中,是一个带有css类“form control”的文本框)。但是,如果我使用合并属性的代码,我不会得到任何与调用
EditorFor
时没有第二个参数不同的结果(即,我的文本框出来时没有设置任何CSS类)

因此,我尝试了其他一些方法,比如将合并的属性强制转换为
对象
。没有变化。我尝试将合并的属性转换为
ExpandoObject
。没有什么。在传入之前,已尝试将
ExpandoObject
强制转换为
对象。没有区别

有没有办法做到这一点?奇怪的是,这只适用于匿名类型,而大多数其他方法(
TextBoxFor
等)都可以使用属性字典


(此时我唯一能想到的就是开始重写/覆盖默认模板以适应这种情况。如果不必这样做,我宁愿不走这条路。)

我认为您需要将htmlAttributes从anonymous更改为RouteValueDictionary

试着这样做:

RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(editorHtmlAttributes);
... + helper.EditorFor(expression, new { htmlAttributes = htmlAttributes }).ToString() + ...

我认为您需要将HtmlatAttributes从匿名改为RouteValueDictionary

试着这样做:

RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(editorHtmlAttributes);
... + helper.EditorFor(expression, new { htmlAttributes = htmlAttributes }).ToString() + ...

为什么RouteValueDictionary会有所不同?这与路由无关。如果你读过那篇文章,它会说在MVC5.1中,他们通过添加匿名对象的使用方式修复了我遇到的问题。在修复过程中,他们碰巧使用了RouteValueDictionary(这篇文章承认,一旦扩展了它的用途,它将是一个可怕的名字)。但是在MVC5中,实现这一点的唯一方法是覆盖默认模板。因此,您的解决方案实际上并不能解决MVC5中的问题。这种情况下的“解决方案”是升级到MVC5.1或更高版本。为什么RouteValueDictionary会有所不同?这与路由无关。如果你读过那篇文章,它会说在MVC5.1中,他们通过添加匿名对象的使用方式修复了我遇到的问题。在修复过程中,他们碰巧使用了RouteValueDictionary(这篇文章承认,一旦扩展了它的用途,它将是一个可怕的名字)。但是在MVC5中,实现这一点的唯一方法是覆盖默认模板。因此,您的解决方案实际上并不能解决MVC5中的问题。这种情况下的“解决方案”是升级到MVC5.1或更高版本。
RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(editorHtmlAttributes);
... + helper.EditorFor(expression, new { htmlAttributes = htmlAttributes }).ToString() + ...