<< back to blog

ASP.NET MVC - Losing the CodeBehind

.net ~ mvc ~ asp.net

11 Nov, 2008

Like some other people, I’m not really enjoying having code behind files in ASP.NET MVC.

As Tim Barcz points out, if you want to lose the CodeBehind and still have strongly typed ViewData.Model, you can do this:

<%@ Page Language="C#" 
           Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]"  %> 
<h1>Welcome <%= ViewData.Model.Username %></h1>

It’s ugly, but it works!

What about another approach?

<%@ Page Language="C#" 
           Inherits="System.Web.Mvc.ViewUserControl"  %>
<% var Model = (LoginData) ViewData.Model; %>
<h1>Welcome <%= Model.Username %></h1>

Again, it’s a kludge, but avoids playing with weird generic names :)

You may also like...
OSX MonoDevelop MVC?
NHibernate: Calling Update Unnecessarily
.NET Config for Multiple Developers
Rake and .NET: The Story Continues
NHibernate in Action 2nd Edition - Your Opinion
Shorthand Repository Accessors
A shout out to Zolv.com - a company that delivers clever stuff!
.NET Circuit Breakers
Automate boring tasks with Rake in .NET
Short n' Sweet NHibernate Sample

kick it on DotNetKicks.com
blog comments powered by Disqus