Los Techies : Blogs about software and anything tech!

Giving Your MVC Views an Identity


One of the great CSS tips I got from Zen of CSS was to put an id on the body tag of your html pages.  This makes it really easy to use one CSS file for your entire site (a optimization trick) and allow you to target elements on specific page easily without creating a lot of unnecessary content wrappers or bogey class names. 

It is also very simple thing to do on MVC sites. Using the convention of Controller+Action give an easy identifier (if you’re using areas or something else you will need to tweak this a little).  This simple extension method does all the work for you.

 

public static class MasterPageExtensions
    {
        public static string BuildPageIdentifier(this ViewMasterPage masterPage,  ViewContext context)
        {

            return string.Format("{0}-{1}", context.RouteData.Values["controller"],context.RouteData.Values["action"]);
        }
    \

Now just add this to your MasterPage.

 

<body id="<%= this.BuildPageIdentifier(this.ViewContext)%>">

It will create html that looks like this.

<body id=”Resident-Edit”>

Now you can write CSS like this to target specific pages on your site.

 

 

#Resident-Edit #VolumeLevel { display:block; margin-bottom:.8em; }

Kick It on DotNetKicks.com
Posted Jul 15 2009, 01:01 AM by jcteague
Filed under: ,

Comments

chadmyers wrote re: Giving Your MVC Views an Identity
on 07-15-2009 2:15 AM

Hah!  I've seen this before, but I always thought it was cheating or a hack.  I'm glad that it's not, but actually recommended!

Summary 15.07.2009 « Bogdan Brinzarea’s blog wrote Summary 15.07.2009 &laquo; Bogdan Brinzarea&#8217;s blog
on 07-15-2009 5:45 AM

Pingback from  Summary 15.07.2009 «  Bogdan Brinzarea’s blog

Ben Scheirman wrote re: Giving Your MVC Views an Identity
on 07-15-2009 8:49 AM

That's a great idea.  This is also recommended in my favorite CSS book, CSS Mastery by Andy Budd.

This can be especially useful for highlighting the current navigation tab automatically with CSS.

igorbrejc.net » Fresh Catch For July 15th wrote igorbrejc.net &raquo; Fresh Catch For July 15th
on 07-15-2009 9:08 AM

Pingback from  igorbrejc.net » Fresh Catch For July 15th

ASP.NET MVC Archived Buzz, Page 1 wrote ASP.NET MVC Archived Buzz, Page 1
on 07-15-2009 11:23 AM

Pingback from  ASP.NET MVC Archived Buzz, Page 1

Aaron Jensen wrote re: Giving Your MVC Views an Identity
on 07-15-2009 3:06 PM

interesting idea.. my only concern is that now renaming controllers or actions requires a css change as well... a subtle coupling that I could see resulting in annoying regressions.

jcteague wrote re: Giving Your MVC Views an Identity
on 07-15-2009 4:18 PM

That's a very good point.  

I will write up a watin test to verify and include it in the post

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - July 14-17, 2009
on 07-16-2009 3:26 AM

Daily tech links for .net and related technologies - July 14-17, 2009 Web Development ASP.NET Membership

The Technology Post for July 16th, 2009 | I love .NET! wrote The Technology Post for July 16th, 2009 | I love .NET!
on 07-16-2009 4:20 PM

Pingback from  The Technology Post for July 16th, 2009 | I love .NET!

The Technology Post for July 16th, 2009 | rapid-DEV.net wrote The Technology Post for July 16th, 2009 | rapid-DEV.net
on 07-17-2009 4:31 AM

Pingback from  The Technology Post for July 16th, 2009 | rapid-DEV.net

Paul wrote re: Giving Your MVC Views an Identity
on 07-17-2009 10:03 AM

@Ben Scheirman

Do you have an example of how this could work?

Cheers!

Andreas wrote re: Giving Your MVC Views an Identity
on 07-17-2009 2:04 PM
mat roberts wrote re: Giving Your MVC Views an Identity
on 07-20-2009 9:03 AM

Excellent.

I was going to work out how to do this myself, but now I can just copy you.

Thanks

Mat

jyefgcflxw wrote re: Giving Your MVC Views an Identity
on 10-04-2009 10:24 AM

DO5N54  <a href="mrthzqsufyyq.com/.../a>, [url=http://ghbgxjrujsvv.com/]ghbgxjrujsvv[/url], [link=http://elsgfhvnyqcs.com/]elsgfhvnyqcs[/link], http://fwjgxgzttced.com/

Add a Comment

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

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems