I'm trying create a custom HTML helper extension method that takes an object htmlAttributes and then get the passed in values (via reflection) and add to them into a Dictionary<string, object>. Unfortunately, this does not work although there is an overload in the InputExtensions class that takes a Dictionary<string, object> htmlAttributes as a parameter.
The problem is that this dictionary is not being processed properly somewhere inside the razor engine (I guess...). Here's how it is being output as 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]"/>
And here's my code:
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 is a follow up to this question.