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?
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:
dynamic o = new ExpandoObject();
o.Stuff = new { Foo = "Bar" }.ToExpando();
return View(o);
And then your view:
@model dynamic
@Model.Stuff.Bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With