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:  }
   5:   
   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


Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

May 31. 2008 10:53

Pingback from weblogs.asp.net

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

weblogs.asp.net

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.

Will

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"]

Haacked

June 1. 2008 07:55

Pingback from alvinashcraft.com

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

alvinashcraft.com

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.

Haacked

June 3. 2008 21:16

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock » The Morning Brew #105

blog.cwa.me.uk

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

code-inside.de

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

matthaw

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

October 9. 2008 19:21

<script type="text/javascript">alert('keke');</script>

aa

October 9. 2008 19:21

good !

aa

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

www.itjoblog.co.uk/.../...-rescues-microsofts.html

itjoblog

June 1. 2009 22:36

Nice blog posts

yazılım

June 3. 2009 12:03

thats kool ya

4396710

June 24. 2009 10:58

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.

fire science degrees

June 24. 2009 10:59

but it is worth it for the additional control you gain.

occupational safety program

June 24. 2009 10:59

things at runtime, but these usages you presented are bad in my option.

Liviu Uba

occupational safety and health online

June 24. 2009 10:59

these usages you presented are bad in my option

fire science degree

June 25. 2009 12:19

thanks for the post

Life Experience Degrees

July 2. 2009 13:13

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.

club penguin

July 18. 2009 21:48

Thank you for useful information was a good article

sex movies

July 20. 2009 14:59

thanks you

Porno izle

August 2. 2009 09:58

hi all.
thank too the apple. http://www.germanporn.tk

Porno filme

August 2. 2009 10:00

Thanks a lot for this scripot.I have been looking for it for a long time http://www.tvsexizle.com

Porno izle

August 4. 2009 09:24

Thank you for having the time and dedication in putting up blogs such as this one! It is deeply appreciated! Keep up the good work!

dentists in Northampton

August 7. 2009 14:33

I am still very new to >net applications but I am trying to learn from developer blogs

Sam

August 9. 2009 11:04

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

tiffany

August 11. 2009 04:43

Do yo know where to buy viagra online without prescription?

viagra online

August 11. 2009 14:27

That's not difficult, but so powerful. Thanks.

sd1200

August 13. 2009 15:09

great article

vigrx plus

August 18. 2009 07:55

Excellent post ! Thanks for sharing.I was trying these things at my end.. and was enjoying it. I've been playing with ASP.NET MVC recently, and it is a delight. Thanks

hunting

August 19. 2009 08:00

great post this stuff is quite entertaining.

car accident lawyer

August 20. 2009 10:50

I like the way you include a different language in your blog, and I know that the persons who can read it will find some useful info in this blog a job well done good post.

online gambling sites

August 24. 2009 11:00

very cool I just found in the ASP.NET MVC Preview 3 bits

Laptop Repair

August 26. 2009 07:02

Great blog. Continue your journey in the cyber space.

cancer treatment

September 8. 2009 02:44

cool, thanks

lpn salary

September 26. 2009 10:24

Like you said, this really works like magic. I have tried your trick and this is really a super cool discovery and I'm excited that you've shared it. Thanks!

Free Internet Advertising

September 28. 2009 20:00

Always leading the avant-garde of fashion without compromising traditional craftsmanship of luxury leather goods
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com ] louis vuitton [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com ] Louis Vuitton Replica [/url]
[url=http://www.tiffanysilverworld.com ] Tiffany Jewelry [/url]
[url=http://www.tiffanycoltd.com ] Tiffany Jewelry [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com ] Louis Vuitton outlet [/url]is also active in other ...Cheap
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com ] Louis Vuitton Replica [/url] Handbags,Purses,Wallets Outlet and
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com ] Louis Vuitton Replica [/url]Designer Bags,Shoes Outlet Store - discount price is our special offer, [url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com ] Louis Vuitton Outlet [/url]
[url=http://www.luxesaler.com ] louis vuitton [/url]
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com ] louis vuitton sale [/url]
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com ] louis vuitton online shop [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com/articles/Louis-Vuitton-Wallets.html ] louis vuitton wallet [/url] is also
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com/products/17495.html ] louis vuitton [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com ] Louis Vuitton Speedy [/url]
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com/articles/Cheap-Louis-Vuitton.html ] cheap louis vuitton [/url]
[url=http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com" rel="nofollow">http://www.favorluxury.com/articles/Discounted-Louis-Vuitton.html ] Discounted Louis Vuitton [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com/articles/Louis-Vuitton-Online-Shop.html ] Louis Vuitton Online Shop [/url]
[url=http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com" rel="nofollow">http://www.louisvuittonlive.com/articles/Louis-Vuitton-Sale.html] Louis Vuitton sale [/url]

cheap louis vuitton

October 6. 2009 06:54

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.

auto insurance

October 8. 2009 06:37

Generic Viagra Online Pharmacy. Buy Viagra Online and Order Cheap Viagra Prescription with Satisfaction Guarantee.

Buy Viagra

October 8. 2009 13:33

Can u compare jsp and asp.net over security issues, fastness, cost effective? Which is best to avoid hacking?

cheap laptop

October 9. 2009 13:42

Do yo know where to buy viagra online without prescription?

viagra supplier

October 9. 2009 13:43

Thank you for having the time and dedication in putting up blogs such as this one! It is deeply appreciated! Keep up the good work!

Buy Kamagra tablets

October 9. 2009 13:57

Hey,
waoo that's really interesting info. I would really love to read more about it.
Thanks

public relations agency

October 9. 2009 14:39

and it is a delight. Yes, there is more manual coding than with Web Forms

live scores online

October 9. 2009 14:40

waoo that's really interesting info

live soccer scores

October 9. 2009 14:41

Great blog. Continue your journey in the cyber space.

best price kamagra

October 9. 2009 14:41

great post this stuff is quite entertaining.

kamagra pills

October 13. 2009 15:44

Thanks for the nice piece of information.
regards
[url=http://www.freehotpennystocks.com/] Penny Stocks[/url]

Penny stocks

October 13. 2009 16:17

it is a great support for doing dynamic things at runtime.Which is best to avoid hacking?

unlock iphone

October 14. 2009 08:40


This is easier and surely gives comfort to internet users. Thanks for sharing. Post like this offers great benefit. Thank you!

Travel Insurance Online

October 14. 2009 09:30

Interesting blog, just bookmarked it for later reference

Abu Dhabi accomodation

October 14. 2009 18:16

Good post.

online games

October 15. 2009 10:55

I like your post & i will always be coming frequently to read more of your post.Thank you very much for your post once more.

control premature ejaculation

October 16. 2009 15:15

I like the way you include a different language in your blog, and I know that the persons who can read it will find some useful info in this blog a job well done good post.

free online games

October 20. 2009 22:29

HOHO,I like it,thank you!

ed hardy

October 21. 2009 13:15

I must say, i have learned a lot of ASP here.

underground electric dog fence

Comments are closed

Copyright © 2000 - 2010 , Excentrics World