Skip to content

Razor anonymous model spike #536

Closed
wants to merge 5 commits into from

2 participants

@Crisfole
Crisfole commented Mar 2, 2012

We bumped into this when trying to do the following in Razor:

<h1>@Model.Name.First</h1>

with the following model:

new { Name = new { First = "Chris" } }

The problem didn't happen when the object was singly-nested and the problem didn't happen if we turned our object into an expando (fully and recursively) first. This is a known limitation of the Razor view engine because the Razor View engine transforms all Views into code within its own assembly. Since C# anonymous objects' members are all internal they are not accessible outside the assembly. Your GetExpandoObject method in DefaultViewFactory was not taking into account the nested object issue.

There is a known problem with the current solution (the failing test introduced by d39c581):

I couldn't figure out a good way to unobtrusively GetSafeModel on Models that have properties that are anonymous and need to be "Expandoed"). Reasons this is hard:
a. Recursing arbitrarily deep in an object could easily result in stackoverflow exceptions
b. Recursing could find a circular reference. (Thus causing #1)
c. Recursing arbitrarily could take a long time, seriously slowing down Nancy's DefaultViewFactory

Crisfole added some commits Mar 1, 2012
@Crisfole Crisfole Add two failing tests
The tests have to do with known Razor view engine limitations that could
be made much more super-duper-happy with some judicious reflection on the
Nancy side of things.
436527e
@Crisfole Crisfole Was testing wrong thing 9fb2652
@Crisfole Crisfole Add tests of 'GetExpandoObject' 1cfd6bf
@Crisfole Crisfole Add another test of ExpandoObject
An object with a dynamic member that's an anonymous class from a foreign
Assembly won't correctly be 'expando-ed'.
d39c581
@Crisfole Crisfole Fix SHA 1cfd6bf 49fd29e
@grumpydev grumpydev closed this Feb 16, 2013
@Crisfole Crisfole deleted the Crisfole:razor-anonymous-model-spike branch Nov 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.