Look Ma, No Code-Behind!

I’ve been playing with some MVC-related stuff and one of the things I took a second to look at was not having to have a code behind in order to do the whole ViewPage<MODEL> business.

I came up with this:




Yes, it works.  Code forthcoming (it’s incubating right now – this is part of a larger chunk of code I do plan on releasing).

Do you like this approach?  Should I continue heading down this path?

About Chad Myers

Chad Myers is the Director of Development for Dovetail Software, in Austin, TX, where he leads a premiere software team building complex enterprise software products. Chad is a .NET software developer specializing in enterprise software designs and architectures. He has over 12 years of software development experience and a proven track record of Agile, test-driven project leadership using both Microsoft and open source tools. He is a community leader who speaks at the Austin .NET User's Group, the ADNUG Code Camp, and participates in various development communities and open source projects.
This entry was posted in ASP.NET, MVC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Take a look at this post:


    This allows you to do the same thing in a declarative fashion. It’s not exactly the prettiest, but it’s another way of accomplishing the same thing, I think.

  • @Joel, although it works, R# chokes on intellisense with CLR notationed inherits attribute (at least mine does)

    @Chad, will you also (at least) demo your code generators and helpers for Thunderdome P. ?Generator for IndexViewModel and this.TextBoxFor stuff etc.

  • Thumbs up for finding new ways to lose the code behind :)

    It raises a question though…

    In Rails, you have Helper classes for encapsulating complex display logic, and lead to simpler views. One place to put these in ASP.NET MVC is in the code behind. Where would you put this stuff without a code behind?

  • Hi Chad,

    yes, I like it and please continue!
    And like Gokhan, I would appreciate if you share some more information of your code arround the “this.TextBoxFor(…)” stuff.
    I like the approach you describe in “Opinionated ASP.NET MVC by Joshua Flanagan”!


  • I work with Johnnie Hall and his approach has been to use the MVC Contrib support:

    < % var model = this.ViewData.Get(); %>

    Seems OK to me.

  • @Joel

    Yeah, I’ve seen that before, but it gets messy and, like Gokhan said, you don’t get intellisense (yet?). At any rate, once the IntelliSense does work, either way will work and it becomes a matter of preference at that point. Hurray choices!

    @Gokhan, Dirk

    I’m working on the code. This is a teaser for what I’m preparing. Please hold on a little longer.

  • @Tobin:

    The way we’re doing it is with microcontrollers which serve a similar purpose.

    We have things like:

    < %= this.TextBoxFor(m=>m.Contact.FirstName).Width(100).Label(“First Name:”) %>

    The TextBoxFor uses an expression builder under the hood and is also aware of our domain model attributes such that “Required” will add a CSS class to the tag so that jQuery validation picks it up. Same for max length and other such things.

  • Dave Cameron

    The microcontroller approach looks great. I’m about to embark on a largish project using ASP.NET MVC. Simplicity of the final source code is a big selling point, so anything to cut-out extra files is great too.

    Really looking forward to the code you are going to post.

  • Looks good. I’m doing something similar with Presentation Model and WPF which I’m working on a post. Will be interesting to see your full post and code and compare notes.

  • This is cool and am looking forward to seeing the code…

    That said, this strikes me as a possible, “oh crap I forgot to set my NHibernate mapping as an embedded resource” issue. Where people don’t get intended behavior until they remember to do some trivial step. I’ve always found this to be a small gripe, but a gripe it is.

    Overall though it’s a step forward and and thankful you’re blazing this trail.

  • @Tim: Although you wouldn’t be able to use the Model field at all in your view code (to bind against, etc). If the developer wants to use the Model field, they HAVE to do this. If they DONT do it, they won’t have access to the Model field. But it won’t matter because they’re not using it!

    There’s no magic “surprise”. You either need it and do it, or don’t and don’t :)

  • I posted an example of how I work around the code-behind issue. I can’t wait to see the code that makes your example work.


  • I immediately remembered articles/notes written by Rob Conery
    and David Safitech
    And also – my own struggle against setting server-side controls properties declaratively on ASPX page