13

Not a duplicate of: MVC Razor dynamic model, 'object' does not contain definition for 'PropertyName'

According to the answers there,

According to David Ebbo, you can't pass an anonymous type into a dynamically-typed view because the anonymous types are compiled as internal. Since the CSHTML view is compiled into a separate assembly, it can't access the anonymous type's properties.

Why does the code below - which allegedly should never work - work as I had expected when the partial view is located in "/Home/_Partial.cshtml", but suddenly stops working when moved to "/Shared/_Partial.cshtml"?

Using ASP.NET 4.5 (and previous versions), the following produces the text "Hello, World!" to the web browser:

~/Controllers/HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestDynamicModel.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

~/Views/Home/Index.cshtml

@Html.Partial("_Partial", new { Text = "Hello, world!", ShouldRender = true } )

~/Views/Home/_Partial.cshtml

@model dynamic
@if (!Model.ShouldRender)
{
    <p>Nothing to see here!</p>
}
else
{
    <p>@Model.Text</p>
}

However, when the _Partial.cshtml is instead moved to ~/Views/Shared/_Partial.cshtml, the following error is thrown in _Partial.cshtml (line 2):

'object' does not contain a definition for 'ShouldRender'

Upon inspecting the Model in the debugger, I find the following properties:

Model { Text = Hello, world!, ShouldRender = True }
Community
  • 1
  • 1

3 Answers3

1

While the question is pertaining to the behavior of ASP.NET MVC, and I am aware of workarounds, I am not sure everyone is. Here is a workaround for anyone who simply wants their code to work: Dynamic Anonymous type in Razor causes RuntimeBinderException

Community
  • 1
  • 1
1

I found this SO answer: https://stackoverflow.com/a/21633464/605067 to be the solution to my error, which had all the same symptoms as yours. The error message was:

'object' does not contain a definition for 'property'

and which resulted from passing an anonymous object to {{@Html.Partial}} in the same manner you did.

As stated in that answer, it's perfectly valid to pass anonymous objects into partial views, and those views do not need to include {{@model dynamic}} - in fact you're creating some unnecessary overhead if you use a dynamic model.

The real problem is that this error message is misleading, at least in my case. The problem was that I had other view files in the same directory that didn't correctly compile, and it seems to throw the view compiler off. The fix provided in the other SO post worked, which was to set the *.csproj propery

<MvcBuildViews>true</MvcBuildViews>

to true, and then fix all the compile errors in my views. After fixing them, I was able to bind to the anonymous object in my partial view.

Community
  • 1
  • 1
crimbo
  • 10,308
  • 8
  • 51
  • 55
-5

To use dynamic type you need to reference Microsoft.CSharp assembly. Check references of your project.

the_joric
  • 11,986
  • 6
  • 36
  • 57
  • I don't think you understand the problem, or even bothered to test the example case provided. Either way, that is not the problem. The same project this problem was encountered in was using dynamics in several places, including the workaround solution I provided in another answer. –  Apr 10 '14 at 17:30