2

I am trying to call the following Editor method from my helper class:

public EditorExtensions {
    public static MvcHtmlString Editor(this HtmlHelper html, string expression, string templateName, object additionalViewData);
}

An example of a call would be:

this.Html.Editor("Name", "TemplateName", new { PropertyId = "Property1" });

This works perfectly fine, until I try to pass a dynamically generated object as the additionalViewData parameter, like an ExpandoObject. The reason why it doesn't work is that the .NET framework will try to do a GetProperties() on the passed Object and ExpandoObject won't retrieve the correct properties as the properties I'm creating at runtime weren't compiled at compile time.

How do I pass dynamically generated information into the additionalViewData parameter?

Andre Pena
  • 56,650
  • 48
  • 196
  • 243

1 Answers1

2

The additionalViewData parameter also often has its clone in the overload which accepts not object but some Dictionary<,> class type.

Well, your ExpandoObject is pretty good at casting to that dictionary type, see it yourself.

this.Html.Editor("Name", "TemplateName",
    (IDictionary<string,object>)YourExpandoObject);

Something like that should work.

AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • Thanks but this doesn't work. As you can see in the documentation, thee's no such overload that accepts IDictionary. Also, passing an IDictionary to the Object parameter will result in the IDictionary to be treated as an Object.. That is.. ASP.NET will do GetProperties() in the IDictionary instead of iterating through the keys – Andre Pena Jun 02 '14 at 22:08