22

I understand from various other related questions here and here among others, that you can't pass anonymously typed objects from the controller to the view because anonymous types are defined with the Internal accessor. The View and Controller code are compiled into different assemblies so attempting to use it results in this error...

object does not contain a definition for 'foo'

That is fine and I can accept that, although it was annoying at first. There are enough suggested workarounds to appease me.

However, I thought you would still be able to pass an anonymous type from a view to a partial view because, both being views, they would be compiled in the same assembly.

Razor View code...

@Html.Partial("Partial1", new { foo = "Something", bar = "Something else" })

and the partial view code for "Partial1"

@model dynamic 

<h1>@Model.foo</h1>
<span>@Model.bar</span>

The strange thing is, this WAS working at the beginning of a the development on a new MVC project, but as I added more views it just stopped working and now give me the same error that I mentioned above.

It is as if I have reached a threshold where the view and partial view are no longer compiled into the same assembly. But I'm just guessing.

I wonder if anyone can shed any light of this.

Community
  • 1
  • 1
Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
  • I've had exactly this problem arise. Dynamics were working with partials, and all of a sudden stopped. – Chris Kemp Feb 29 '12 at 10:00
  • I never found out why. Had to just stop using dynamic types. Because there was such a simple workaround I couldn't justify any more time spent investigating it – Andy McCluggage Feb 29 '12 at 10:59
  • I'm having exactly the same problem ! Has anyone found an explanation yet ? – Arno 2501 Jan 22 '13 at 09:55
  • 6
    As an alternative, you can pass in a dictionary object like `@Html.Partial("Partial1", new Dictionary {{ "var1", "val1" }, { "var2", "val2" }}`, and then in the partial you can reference it via `@Model["var1"]` – jbyrd Feb 04 '16 at 18:03

4 Answers4

13

Don't know the reason why it stopped working but here is the workaround.

Use @ViewData.Eval("foo") instead of @Model.foo

and remove your @model dynamic line. There is no need of it.

adeel41
  • 3,123
  • 1
  • 29
  • 25
2

For full details please see the question and my answer here:

MVC Razor dynamic model, 'object' does not contain definition for 'PropertyName'

Essentially, the most likely reason it stopped working is because you have another view in the same folder with a model type that is not resolvable.

Correct the offending view with the broken model type, clean and rebuild the solution and it should work again.

Community
  • 1
  • 1
joshcomley
  • 28,099
  • 24
  • 107
  • 147
2

I was never able to explain why this was working and then stopped, so simply had to resort to using named type definitions. Not the end of the world but disappointing. This kind of thing is perfectly suited to using dynamic types.

Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
  • 1
    That was exactly my opinion. It was too much of a distraction and didn't justify my time, given that I had a working alternative (i.e. strongly typed). – Chris Kemp Feb 29 '12 at 13:07
1

In ASP.NET Core, you can call the partial view with a partial view tag helper:

<partial name="MyPartialView" model='new { SomeProperty = "some value" }' />

then, in the partial view, simply omit the @Model directive and, voila, your Model simply shows up. So, in the partial view, you can do something like this:

@{ var someProperty = Model.SomeProperty; }
Bob.at.Indigo.Health
  • 11,023
  • 13
  • 64
  • 111
  • Huh. I don't even remember writing this answer. I guess this means that, lurking somewhere in my code, I have an instance of this construct that no longer works... – Bob.at.Indigo.Health Aug 17 '20 at 15:28