【问题标题】:Modifying HTML attributes passed to custom HtmlHelper extensions修改传递给自定义 HtmlHelper 扩展的 HTML 属性
【发布时间】:2012-05-17 14:34:32
【问题描述】:

我正在尝试创建一个自定义 HTML 帮助程序扩展方法,该方法采用 object htmlAttributes,然后获取传入的值(通过反射)并将它们添加到 Dictionary<string, object> 中。不幸的是,尽管InputExtensions 类中有一个将Dictionary<string, object> htmlAttributes 作为参数的重载,但这不起作用。

问题是剃须刀引擎内部某处没有正确处理该字典(我猜......)。以下是它作为 HTML 输出的方式:

<input name="FirstName" id="FirstName" type="text" Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" Keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" Count="3" Comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]"/>

这是我的代码:

    Dictionary<String, Object> attributes = new Dictionary<String, Object>();
    attributes.Add("readonly", "readonly");
    PropertyInfo[] properties = htmlAttributes.GetType().GetProperties();
    foreach (PropertyInfo propertyInfo in properties)
    {                                        
        if (propertyInfo.Name.Equals("class"))
        {
            attributes.Add("class", String.Format("{0} {1}", "readOnly", propertyInfo.GetValue(htmlAttributes, null)));
        }
        else
        {
            attributes.Add(propertyInfo.Name, propertyInfo.GetValue(htmlAttributes, null));
        }
    }

    genericMethod = methodInfo.MakeGenericMethod(new[] { typeof(TModel), typeof(TProperty) });
    result = genericMethod.Invoke(null, new object[] { helper, expression, (Dictionary<String, Object>)attributes }) as MvcHtmlString;

P.S:这是对this question.的跟进

【问题讨论】:

    标签: c# asp.net-mvc-3 razor html-helper


    【解决方案1】:

    您应该能够直接调用 TextBoxFor 方法并传入表达式和新的 html 属性。

    public static IHtmlString Test<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        Dictionary<String, Object> attributes = new Dictionary<String, Object>();
        attributes.Add("readonly", "readonly");
        PropertyInfo[] properties = htmlAttributes.GetType().GetProperties();
        foreach (PropertyInfo propertyInfo in properties)
        {
            if (propertyInfo.Name.Equals("class"))
            {
                attributes.Add("class", String.Format("{0} {1}", "readOnly", propertyInfo.GetValue(htmlAttributes, null)));
            }
            else
            {
                attributes.Add(propertyInfo.Name, propertyInfo.GetValue(htmlAttributes, null));
            }
        }
    
        //call the input tag
        return helper.TextBoxFor(expression, htmlAttributes);
    }
    

    【讨论】:

    • 还有一个RouteValueDictionary 构造函数重载,它也接受object。无需使用反射。
    • 重载只是做同样的事情,但你是对的,你可以更轻松地使用它。
    • 我尝试创建一个带有属性字典的RouteValueDictionary 实例,然后将该实例作为被调用方法的参数传递,但这仍然没有改变任何东西!我需要做的就是能够修改htmlAttributes 对象,所以如果我能完全摆脱字典那就完美了。
    • 好的,我明白你的问题了。你想从你的扩展中返回什么?只是html属性的字符串还是整个标签本身?
    猜你喜欢
    • 2013-03-31
    • 2015-11-07
    • 2011-12-31
    • 1970-01-01
    • 2011-06-25
    • 2012-06-27
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    相关资源
    最近更新 更多