Tim Barcz

Sponsors

The Lounge

Groups and Affiliations

Syndication

News

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Strongly-Typed ViewData Without A Codebehind

image  I'm not a fan of the codebehind and designer files that are generated when you create a new ViewPage.  I like a clean solution and for me that means view files (.aspx) that don't have plusses next to them (see the photo to the right and compare the Home and Login folders).

If you want to use strongly typed view data and you don't want to add the codebehind and designer files for the measly few characters you have to type to make a view strongly typed:

   1: public partial class Index : ViewPage<LoginData>
   2: {
   3: }

In this particular case I have to add two extra files to source control solely for the purpose of having "<LoginData>"...seems like a waste.

CLR Notation

If you want to do the same thing without the extra overhead of two files, simply use CLR notation.  The old inherits attribute:

   1: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
   2:     CodeBehind="Index.aspx.cs" 
   3:     Inherits="ABCCompany.MVC.Web.Views.Home.Index" %>

becomes

   1: <%@ Page Language="C#"
   2:     MasterPageFile="~/Views/Shared/Site.Master" 
   3:     Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]" %>

Not the prettiest code I've ever seen, but it gets the job done without extra (needless) files.


Posted 08-13-2008 3:19 PM by Tim Barcz

[Advertisement]

Comments

Tyrone wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-13-2008 5:33 PM

So, how did you get rid of all the code-behing file? Did you manually delete the files or did you use some sort of macro to do it?

Also, It's probably a good thing modify the MVC Project Templates and remove the extra code-behind files if this will be a common thing.

Thanks. This was getting on my nerves too.

Tim Barcz wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-13-2008 5:38 PM

@Tyrone,

At this point in time I have only a few views which have the codebehind so I just delete it (don't forget to delete the Codebehind attribute from the .aspx.

Derik Whittaker wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-13-2008 6:26 PM

I like this.  not nice to look at, but works.

Chris Sutton wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-14-2008 1:44 AM

I'm hoping that the next big release of ASP.NET will let us use straight C#/VB syntax for this scenario.

Chris

Jak Charlton wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-14-2008 2:32 AM

A very cool idea ... I had been thinking "some idiot is going to put code in the aspx.cs file" and this is a neat way to stop it :)

(oh and welcome to devlicio.us !)

Dew Drop - August 14, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - August 14, 2008 | Alvin Ashcraft's Morning Dew
on 08-14-2008 9:52 AM
Pingback from Dew Drop - August 14, 2008 | Alvin Ashcraft's Morning Dew
Ben Scheirman wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-14-2008 2:48 PM

I talked to the VS team about this very thing, and the response was "that's a HUGE change" ..  There's very little chance we will get any better syntax for this.

I will agree with Tim & say that code behind is mostly useless now (and it just tempts you to put code there), I have fully adopted MvcContrib's ViewDataExtensions, so I have no need for strongly typed views.

ViewData.Get<T>()   is pretty concise and gives me what I want.  You can further reduce the noise if you add a custom BaseViewPage with a Get<T> method on it that defers to ViewDataExtensions.

Arjan`s World » LINKBLOG for August 14, 2008 wrote Arjan`s World &raquo; LINKBLOG for August 14, 2008
on 08-14-2008 3:03 PM

Pingback from  Arjan`s World    &raquo; LINKBLOG for August 14, 2008

Tim Barcz wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-14-2008 3:11 PM

@Ben

The notation, while messy, doesn't necessarily bother me. I've been using Windsor for awhile and generic declarations in Windsor (xml config) can get pretty nasty.

I do want to thank you (and the rest of the MvcContrib team) for a lot of the extensions, they're handy

ASP.NET MVC Archived Buzz, Page 1 wrote ASP.NET MVC Archived Buzz, Page 1
on 08-15-2008 7:06 AM

Pingback from  ASP.NET MVC Archived Buzz, Page 1

Chris Sutton wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-15-2008 5:40 PM

@Ben,

I wouldn't have suspected that better syntax in the aspx would have been so complex. I'm definitely keeping everything out of the code-behind regardless. Like you said it's too easy to start putting logic in there.

I'll have to check out the ViewDataExtensions a bit more.

Chris

ASP.NET MVC and Codebehind Files at { null != Steve } wrote ASP.NET MVC and Codebehind Files at { null != Steve }
on 09-23-2008 4:56 PM

Pingback from  ASP.NET MVC and Codebehind Files at { null != Steve }

QLeelulu wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-07-2008 11:39 PM

if i need ViewPage<List<BlogEngine.Core.IPublishable>>,

and the code:

Inherits="System.Web.Mvc.ViewPage`1[[System.Collections.Generic.List`1[[BlogEngine.Core.IPublishable,BlogEngine.Core]],System.Collections.Generic]]"

but this is wrong, how can i do this?

Tim Barcz wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-07-2008 11:46 PM

@QLeelulu

Notice that the CLR notation is <fully-qualified-class, assembly>

You have System.Collections.Generic.List (class) but the assembly is incorrect.  Should be System, not System.Collections.Generic

QLeelulu wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-08-2008 12:13 AM

but i change to

Inherits="System.Web.Mvc.ViewPage`1[[System.Collections.Generic.List`1[[BlogEngine.Core.IPublishable,BlogEngine.Core]],System]]"

it still wrong

QLeelulu wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-08-2008 1:02 AM

Inherits="System.Web.Mvc.ViewPage`1[[System.Collections.Generic.List`1[[BlogEngine.Core.IPublishable,BlogEngine.Core]],mscorlib]]

with this code it work.the assembly should be mscorlib.

thanks for your help!

Busby SEO Test wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-13-2008 3:18 AM

Thanks for sharing your ideas! Tenara.

mogadanez wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-19-2008 6:50 AM

i use this notation fro preview 3.

but have one annoying thing: Resharper don't understand this. =(

jetbrains.net/.../RSRP-77920

you've been HAACKED wrote A Little Holiday Love From The ASP.NET MVC Team
on 12-19-2008 12:48 PM

A Little Holiday Love From The ASP.NET MVC Team

ASP.NET MVC Design Gallery y un primer vistazo a las futuras mejoras de ASP.NET MVC Release Candidate « Thinking in .NET wrote ASP.NET MVC Design Gallery y un primer vistazo a las futuras mejoras de ASP.NET MVC Release Candidate &laquo; Thinking in .NET
on 12-24-2008 1:23 PM

Pingback from  ASP.NET MVC Design Gallery y un primer vistazo a las futuras mejoras de ASP.NET MVC Release Candidate &laquo; Thinking in .NET

Patrick A. Lorenz wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-28-2008 2:38 AM

Hi Tim,

I like the idea. Instead of using the CLR notation you could you a PageParserFilter to interfer a type notation conversion. Please send me an email to lorenz ... pgk.de and I'll provide you with a sample.

Best,

Patrick

Kabon wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-03-2009 6:18 AM

Hi Tim,

I totally agree with you. it may make our work more easily.

Bisnis dari Rumah wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-03-2009 4:53 PM

thanks for sharing the info :)

Busby SEO Test wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-13-2009 10:08 AM

Great Tutorial, And It's work dude. Thanks for sharing this

Inventory Management Software wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-20-2009 9:11 PM

yeah .. this work great for me.. love your info..

Busby SEO Test wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-29-2009 4:27 AM

That good tutorial dude, keep work it.

Free Registry Cleaners wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-13-2009 9:47 PM

this tutorial is easy to understand i love it. great post.

Hectic Capiznon Bloggers 2009 wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-17-2009 2:13 AM

nice information thanks for sharing.

Sample Weblog wrote 16、ASP.NET MVC设计陈列室和即将推出的ASP.NET MVC RC版本中的视图方面的改进
on 02-18-2009 11:01 AM

【原文地址】 ASP.NET MVC Design Gallery and Upcoming View Improvements with the ASP.NET MVC Release Candidate

Learn Selling wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-22-2009 8:12 AM

wow, thank you for sharing this. keep it up!

the peoples program team wrote re: Strongly-Typed ViewData Without A Codebehind
on 03-11-2009 5:30 AM

thanks for the code.,. looking forward for more ideas.,(^_^)

cooldude055 wrote re: Strongly-Typed ViewData Without A Codebehind
on 03-14-2009 5:11 AM

Thanks for sharing this one, keep it up.

Hectic Capiznon Bloggers 2009 wrote re: Strongly-Typed ViewData Without A Codebehind
on 03-27-2009 12:44 AM

Great information and tutorial its working.. thanks for sharing.

goodpeoplegives wrote re: Strongly-Typed ViewData Without A Codebehind
on 04-14-2009 2:53 AM

This is a great tutorial, thanks for sharing this one.:)

goodpeoplegives wrote re: Strongly-Typed ViewData Without A Codebehind
on 04-14-2009 2:57 AM

Thanks for sharing this great tutorial.

billrainier wrote re: Strongly-Typed ViewData Without A Codebehind
on 04-14-2009 3:20 AM

Keep up your good work.:)

auto repair manual wrote re: Strongly-Typed ViewData Without A Codebehind
on 04-29-2009 12:59 PM

I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

tukang nggame wrote re: Strongly-Typed ViewData Without A Codebehind
on 05-07-2009 4:14 AM

What is strongly typed view data?

tukang nggame wrote re: Strongly-Typed ViewData Without A Codebehind
on 05-07-2009 4:17 AM

Yes that is brrrrrrrilliannnnnnnnt

sulumits retsambew wrote re: Strongly-Typed ViewData Without A Codebehind
on 05-08-2009 3:52 PM

very nice info, thanks.

Simulation pret immobilier wrote re: Strongly-Typed ViewData Without A Codebehind
on 05-13-2009 2:18 AM

Thanks for sharing this great article!

Tukang Nggame wrote re: Strongly-Typed ViewData Without A Codebehind
on 05-24-2009 2:27 PM

Wow, I never knew that, thank for informing.

Music Careers wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-06-2009 12:43 AM

That was awesome! really love it!

seo company wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-11-2009 5:23 AM

Excellent post.Thanks for your information!!!!!!

Rusli Zainal wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-16-2009 7:29 PM

I like this post, thanks :-)

rus wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-16-2009 7:31 PM

Thanks for informatio, I like it....

Edy wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-16-2009 7:36 PM

I like it, it's great information, thanks very much

Vis wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-16-2009 7:37 PM

Oke, I need it. Thanks

Belajar seo wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-21-2009 4:27 AM

really help me, thank's for sharing

stop dreaming start action wrote re: Strongly-Typed ViewData Without A Codebehind
on 08-31-2009 2:40 AM

thnks 4 sharing your idea bro...

this is useful 4 me....

Oes Tsetnoc wrote re: Strongly-Typed ViewData Without A Codebehind
on 09-19-2009 1:21 PM

Wow, I didn't think about that before. Thanks for informing.

Kenali dan Kunjungi Objek Wisata di Pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 09-28-2009 6:54 AM

Thanks for this valuable information. Regards!

Oes Tset wrote re: Strongly-Typed ViewData Without A Codebehind
on 09-28-2009 6:56 AM

Thanks for sharing this info.

oes tsetnoc wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-13-2009 9:29 AM

thanks for the article..

sure it can help me..

Kenali dan Kunjungi Objek Wisata di Pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-17-2009 4:07 AM

I can here the new knowledge.

Thanks for the great reference post.

Blog SEO wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-17-2009 4:10 AM

Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this.

Cah Bagoes wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-17-2009 4:13 AM

I personally like your post. It is very good to know that you don’t know. Fantastic post! Keep posting your good work.

Cah Bagoes wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-17-2009 4:14 AM

I personally like your post. It is very good to know that you don’t know. Fantastic post! Keep posting your good work.

Kerja Keras Adalah Energi Kita wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-18-2009 5:01 AM

Thanks for this nice info, it's so useful for me.

Kerja Keras Adalah Energi Kita wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-19-2009 8:13 AM

Nice posting... make me new inspirations :)

kenali dan kunjungi objek wisata di pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 10-26-2009 6:50 AM

Thanks for this nice info!

Serunya Belajar SEO wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-01-2009 8:53 AM

I don’t know If I said it already but this so good stuff keep up the good work.

Kenali dan Kunjungi Objek Wisata di Pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-01-2009 9:09 AM

I read a lot of blogs on a daily basis and for the most part just wanted to make a quick comment to say I’m glad I found your blog. Thanks.

pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-02-2009 5:05 PM

great blog

Belajar Blogging wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-04-2009 11:15 AM

hello, this is my first time i visit here. I found so many interesting in your blog especially its discussion. keep up the good work.

Kenali dan Kunjungi Objek Wisata di Pandeglang wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-05-2009 4:16 AM

very cool site site! I am loving it!! Will come back again - taking you feeds also, Thanks.

Kerja Keras Adalah Energi Kita wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-05-2009 4:18 AM

I am thankful for such a great post with many new things to learn. Thanks, you cleared up some things for me.Just what I was googling for! Thanks for sharing!

shutters wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-08-2009 3:39 PM

Good clean code  - it really does the job. Thanks for sharing

Mbah Gendeng wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-13-2009 8:58 AM

I personally like your post. It is very good to know that you don’t know. Fantastic post! Keep posting your good work.

fifa world cup wallpaper wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-14-2009 10:08 AM

thanks guy for the posting, it's very usefull for me

fifa world cup wallpaper wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-14-2009 10:10 AM

thanks guy for the posting, it's very usefull for me

Kerudung Paris wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-21-2009 4:37 AM

Hi, interesting post. I have been wondering about this issue,so thanks for posting. I’ll likely be coming back to your blog. Keep up great writing.

Grosir Jilbab wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-21-2009 4:39 AM

nice post....there so many interesting thing in this blog. keep posting, i really want to comeback

cartoon wallpaper wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-24-2009 9:42 PM

thanks guy for the posting, it's very usefull for me

link building packages wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-26-2009 3:06 AM

Thanks for these valuable infos. I love your post.

internet marketing services wrote re: Strongly-Typed ViewData Without A Codebehind
on 11-26-2009 5:41 AM

Great post! Thanks for this!

Interior Design wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-04-2009 3:24 PM

thank for the informations

Oes Tsetnoc wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-06-2009 10:15 AM

this is great information that i know a lot of people are interested in, looking forward to the next entries.

bali car rental wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-10-2009 11:26 PM

You got it man.

Vienka wrote re: Strongly-Typed ViewData Without A Codebehind
on 12-25-2009 1:58 PM

very interesting , thanks for share

Indonesia Java International Destination wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-05-2010 7:26 AM

While surfing Yahoo I clicked on your link...very informative! I enjoy staying up-to-date about this kind of info. I'll definitely bookmark your site for additional review.

Search Engine Optimisation wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-14-2010 7:18 AM

Wow,  I never thought of this before. Just amazing, thanks for sharing.

SEO

Search Engine Optimisation wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-14-2010 7:19 AM

Thanks for sharing this weird but interesting concept.

SEO

special area family guide wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-15-2010 12:35 PM

There are many things should be taken into consideration, but you've made a good point here. Thanks a lot for that. I will follow your way soon.

Genduk wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-16-2010 10:20 AM

cool! great ideas. I've never seen anything like this. thanks

car new wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-17-2010 8:02 AM

that is good idea, as my idea before

official top 5 fighter list wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-17-2010 8:16 AM

There are many things should be taken into consideration, but you've made a good point here. Thanks a lot for that. I will follow your way soon.

Nafa wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-26-2010 5:24 PM

Great information, thank you very much for share

Shutters for windows wrote re: Strongly-Typed ViewData Without A Codebehind
on 01-27-2010 7:49 AM

Great post and really handy code tip thank you

sikat ang pinoy wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-03-2010 3:55 AM

valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up!

renantech wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-05-2010 12:59 AM

This is really a well laid out website. I like how you have presented the information in full detail. Keep up the great work

belajar ngeblog wrote re: Strongly-Typed ViewData Without A Codebehind
on 02-07-2010 9:52 PM

thanks for sharing admin. Love it so much.

regards, belajar ngeblog

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)