I want to use an ExpandoObject as the viewmodel for a Razor view of type ViewPage<dynamic>
. I get an error when I do this
ExpandoObject o = new ExpandoObject();
o.stuff = new { Foo = "bar" };
return View(o);
what can I do to make this work?
I want to use an ExpandoObject as the viewmodel for a Razor view of type ViewPage<dynamic>
. I get an error when I do this
ExpandoObject o = new ExpandoObject();
o.stuff = new { Foo = "bar" };
return View(o);
what can I do to make this work?
I stand corrected, @gram has the right idea. However, this is still one way to modify your concept.
Edit
You have to give
.stuff
a type sincedynamic
must know what type of object(s) it's dealing with..stuff
becomes internal when you set it to an anonymous type, so@model dynamic
won't help you hereAnd, of course, the
MyTypedObject
:Try setting the type as
dynamic
Go through this excellent post on ExpandoObject
Using the open source Dynamitey (in nuget) you could make a graph of ExpandoObjects with a very clean syntax;
You can do it with the extension method mentioned in this question:
Dynamic Anonymous type in Razor causes RuntimeBinderException
So your controller code would look like:
And then your view: