<< 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...
.NET Gem Fun
Running FubuMVC on Mono (OSX)
Really Easy Way To Partition an ASP.NET MVC With An Admin Area
Automating .NET development (and NHibernate) with IronRuby + Rake
Get a Rails-Stylee Interactive Shell For Your NHibernate Backed Domain Model
In Memory SQLite Testing with NHibernate
NHibernate Trick
OSX MonoDevelop MVC?
NHibernate: Calling Update Unnecessarily
.NET Config for Multiple Developers

kick it on DotNetKicks.com
blog comments powered by Disqus