eWorld.UI - Matt Hawley

Ramblings of Matt

ViewData "dot" Notation Expressions in ASP.NET MVC

May 29, 2008 06:48 by matthaw

Here's something very cool I just found in the ASP.NET MVC Preview 3 bits, you can specify, what I call, "dot" notation expressions on your view data. Say you had the following model:

   1:  public class Bar {
   2:     public int Id { get; set; }
   3:     public string Name { get; set; }
   4:  }
   6:  public class Foo {
   7:     public int Id { get; set; }
   8:     public string Name { get; set; }
   9:     public Bar TheBar { get; set; }
  10:  }

When you define your view, you specify Foo as the type of your model. If you cared about type safety prior to run-time, you'd probably write your code in your view like:

<%= Html.Encode(ViewData.Model.TheBar.Name) %>

However, with the new ViewDataDictionary you can now use the "dot" notation expressions to get access the same. As you'll see, you're simply using the indexer of your ViewData object, which internally performs a data-binding eval like operation to search for the key. It is operating in a way that it first will access any specific ViewData items set within your controller, and if that is not found will attempt to perform an eval against your the Model. Using the following controller action:

   1:  public class FooController : Controller {
   2:     public ActionResult View() {
   3:        Foo foo = new Foo() { Id = 1, 
   4:                              Name = "My Foo",
   5:                              TheBar = new Bar() {
   6:                                  Id = 12,
   7:                                  Name = "My Bar"
   8:                              }
   9:                            };
  10:        return View(foo);
  11:     }
  12:  }

You would write the following syntax in your view:

<%= Html.Encode(ViewData["TheBar.Name"]) %>

Given that you have not set the following,

ViewData["TheBar.Name"] = "My Bar";

within your code, you'll be surprised that this works like magic! However, if for any reason you do set the prior ViewData key in the controller action, it's value will be displayed instead of going to your model. Going even further, it doesn't necessarily always have to go off of your Model either, as

ViewData["FooObj"] = foo;

would allow you to access the members with the following expression within your view:

<%= Html.Encode(ViewData["FooObj.TheBar.Name"]) %>

Wow, super-powerful :) You gotta love that syntactical sugar!

kick it on DotNetKicks.com


May 31. 2008 10:53

Pingback from weblogs.asp.net

ViewData "dot" Notation Expressions in ASP.NET MVC - eWorld.UI - Matt Hawley


May 31. 2008 11:19

I've noticed that databinding in MVC is following the patterns laid down by WPF pretty closely.  I get the feeling they'll eventually provide for almost all of the same functionality of the DataContext model (flow of data through parent controls to child controls) and binding syntax (like the dot notation you described here), whether it is exactly the same or a very similar pattern in MVC.  Which is pretty damn cool.


May 31. 2008 13:59

The dot notation also handles dictionary lookup as well. For example:

ViewData["Root"] = new {Foo = new {Bar = "Test"}};

You can do this: ViewData["Root.Foo.Bar"]


June 1. 2008 07:55

Pingback from alvinashcraft.com

Dew Drop - May 30, 2008 | Alvin Ashcraft's Morning Dew


June 3. 2008 01:09

I think that using   View<T> and having compile time checking is far useful than having magic string lookups.
Should I write test to spelling errors in my string expressions? Is this the next generation software?

Liviu Uba

June 3. 2008 07:58

@Liviu then don't use the ViewData dot notation and use the ViewPage<T>. Nobody's stopping you.


June 3. 2008 21:16

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock  » The Morning Brew #105


June 4. 2008 10:31

Pingback from code-inside.de

Wöchentliche Rundablage: ASP.NET MVC, .NET, ADO.NET Data Services, Silverlight, WPF… | Code-Inside Blog


June 4. 2008 10:46

Why advertise programming styles that are not safe as latest new and exceptional stuff?

Liviu Uba

June 4. 2008 11:22

Who say's it's not safe? You control your code


June 4. 2008 17:43

Oh, it's as safe as: string.format("{0}{1}{2}", "safe", "programming");
I get it, this is the future...

Liviu Uba

June 4. 2008 17:47

I may agree it is a great support for doing dynamic things at runtime, but these usages you presented are bad in my option.

Liviu Uba

April 17. 2009 05:28

Excellent post ! Thanks for sharing.  I've been playing with ASP.NET MVC recently, and it is a delight. Yes, there is more manual coding than with Web Forms, but it is worth it for the additional control you gain.

I really like Tim Anderson's views on ASP.NET MVC on ITJOBLOG, if you fancy a good read....



Comments are closed

Copyright © 2000 - 2023 , Excentrics World