ASP.NET MVC的助手,合并两个对象htmlAttributes在一起 [英] ASP.NET MVC Helpers, Merging two object htmlAttributes together
问题描述
我有一个情况我需要编写一个HTML助手为扩展另一个HTML帮手。通常情况下,助手是这样的。
I have a situation where I need to write an HTML Helper to extend another html helper. Normally, the helper would look like this.
@ Html.TextAreaFor(型号=> model.Content,新{@class =一些CSS,@data_bind =一些其他的东西......})
此工作得很好,但它有被包裹在一些其他HTML,始终是相同的。我想它封装为方便起见,是这样的。
This works fine, but it has to be wrapped in some other HTML that is always the same. I wanted to encapsulate it for convenience, like this.
public static MvcHtmlString CondensedHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) {
var stringBuilder = new System.Text.StringBuilder();
var tag = new TagBuilder("div"); tag.AddCssClass("some_css");
stringBuilder.Append(toolbar.ToString(TagRenderMode.SelfClosing));
stringBuilder.Append(htmlHelper.TextAreaFor(expression, htmlAttributes));
// more tags and such...
return new MvcHtmlString(stringBuilder.ToString());
}
行 stringBuilder.Append(htmlHelper.TextAreaFor ...
就是我要改变,这已去的CSS类有总是将是present,所以我宁愿在这里包含它。但是我想能够指定顶级助手额外的CSS类。所以......
The line stringBuilder.Append(htmlHelper.TextAreaFor...
is what I want to change. The CSS class that has to go there is always going to be present. So I would rather include it here. However I would like to be able to specify additional CSS classes in the top-level helper. So ...
@ Html.CondensedHelperFor(型号=&GT; model.Content,新{@class =some_other_css})
和静态CSS,这将永远在那里度过的助手笼罩研究。
And the static css that will always be there get blanketed in through the Helper.
任何想法?
推荐答案
首先,创建一个方法(最好是创建一个扩展方法),通过反射型转换的目的是IDictionary的:
First, create a method (the best would be to create an extension method) that converts an object to IDictionary via type reflection:
public static IDictionary<string, object> ToDictionary(this object data)
{
if(data == null) return null; // Or throw an ArgumentNullException if you want
BindingFlags publicAttributes = BindingFlags.Public | BindingFlags.Instance;
Dictionary<string, object> dictionary = new Dictionary<string, object>();
foreach (PropertyInfo property in
data.GetType().GetProperties(publicAttributes)) {
if (property.CanRead) {
dictionary.Add(property.Name, property.GetValue(data, null));
}
}
return dictionary;
}
现在,利用C#4.0 ExpandoObject,它允许在运行时添加属性。 你最终会是这样的:
Now, make use of C# 4.0 ExpandoObject, which allows adding properties at runtime. You would end up with something like this:
public static MvcHtmlString CondensedHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) {
{
var dictAttributes = htmlAttributes.ToDictionary();
var result = new ExpandoObject();
var d = result as IDictionary<string, object>; //work with the Expando as a Dictionary
if(dictAttributes != null)
{
foreach (var pair in dictAttributes)
{
d[pair.Key] = pair.Value;
}
}
// Add other properties to the dictionary d here
// ...
var stringBuilder = new System.Text.StringBuilder();
var tag = new TagBuilder("div"); tag.AddCssClass("some_css");
stringBuilder.Append(toolbar.ToString(TagRenderMode.SelfClosing));
stringBuilder.Append(htmlHelper.TextAreaFor(expression, result));
return new MvcHtmlString(stringBuilder.ToString());
}
这篇关于ASP.NET MVC的助手,合并两个对象htmlAttributes在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!