6

Possible Duplicate:
Dynamic Anonymous type in Razor causes RuntimeBinderException

I am trying to use a dynamic type model in my MVC application. I have the following code: in controller:

var model = new { Name = "test name", Family = "m" };
return this.View(model);

and in the view I have:

@model dynamic

@if(Model!=null)
{
   <p> @Html.Raw(Model.Name) </p>
}

When I am running this, I am getting the following error :

'object' does not contain a definition for 'Name'   (System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

Why do I get this error? During debug, if I put my cursor on @Model, I can see that it has two property called Name and Family.

Community
  • 1
  • 1
mans
  • 17,104
  • 45
  • 172
  • 321

1 Answers1

16

What you have shown is not a dynamic type. It's an anonymous type. There's a huge difference.

You cannot use anonymous types as models. The reason for this is because the compiler emits anonymous types as internal. This means that they are only accessible withing the current assembly. But as you know Razor views are compiled by the ASP.NET runtime as separate assemblies which have no way of using those anonymous types.

Obviously the correct way in this case is to use a view model:

public class MyViewModel
{
    public string Name { get; set; }
    public string Family { get; set; }
}

and then have your controller action pass this view model to the view:

var model = new MyViewModel { Name = "test name", Family = "m" };
return this.View(model);

so that your view can work with it:

@model MyViewModel
@if (Model != null)
{
    <p>@Model.Name</p>
}

Some people (not me, I would never recommend anything like this) also use ViewBag and this way they don't need a model:

ViewBag.Name = "test name";
ViewBag.Family = "m";
return this.View();

and then in the view:

<p>@ViewBag.Name</p>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for explanation. I am wondering how ViewBag is implemented? Can I have a dynamic model? this post says that it is possible: http://blogs.msdn.com/b/rickandy/archive/2011/01/28/dynamic-v-strongly-typed-views.aspx but I could not do it. – mans Jan 11 '12 at 15:07
  • @user654019, ViewBag is just a dynamic wrapper around the ViewData dictionary. You need to have some concrete type to be used as model. You could use `@model dnyamic` but if this model is not pointing to a specific type (not anonymous object, it can't work with anonymous objects for the reasons I outlined in my answer) it simply won't work. In the blog post you have linked they are pointing the dnyamic model to concrete classes they defined like `Blog`, `List`, all being strongly typed. There is not a single anonymous object. You can forget about them in ASP.NET MVC. – Darin Dimitrov Jan 11 '12 at 15:17
  • Thanks, Can you please give me some clue on how Viewbag is implemented? Any document? – mans Jan 11 '12 at 15:22
  • @user654019, it's extremely simple, a couple of lines of code. It's just a wrapper around ViewData. You can download the [ASP.NET MVC 3 source code](http://aspnet.codeplex.com/releases/view/58781) and look at the `DynamicViewDataDictionary.cs` file. It derives from DynamicObject and wraps the ViewData, so when you do `ViewBag.Foo` it will invoke the underlying `ViewData["Foo"]`. – Darin Dimitrov Jan 11 '12 at 15:31