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.

4
  • I've had exactly this problem arise. Dynamics were working with partials, and all of a sudden stopped. Commented Feb 29, 2012 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 Commented Feb 29, 2012 at 10:59
  • I'm having exactly the same problem ! Has anyone found an explanation yet ? Commented Jan 22, 2013 at 9:55
  • 6
    As an alternative, you can pass in a dictionary object like @Html.Partial("Partial1", new Dictionary<string, string> {{ "var1", "val1" }, { "var2", "val2" }}, and then in the partial you can reference it via @Model["var1"] Commented Feb 4, 2016 at 18:03

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice. I have read alot on this distressing subject. Folks writing extensions and all sorts. This is a real easy solution.
How can it be used in List of Model?
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.

Comments

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.

1 Comment

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).
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; }

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.