views:

2075

answers:

3

Hello, I wondered how I can stop those annoying compiler warnings "The class or CssClass value is not defined" coming from my ASP.NET MVC partial views/ASCX user controls. The CSS is correctly defined, but the reference to the CSS file is obviously on the master page only. My partial views contain lots of CSS class references on div's etc. and so I get massive amounts of warnings which I shouldn't see.

How can I solve this?

Thank you !

+5  A: 

One way is to turn HTML Syntax Checking off (Tools->Options->Text editor->HTML->Show Errors->In CSS).

I use the Firefox Tidy plug in, which gives better advice, IMHO.

Craig Stuntz
+8  A: 

Include this in your partial view:

<% if (false) { %>
   <link rel="stylesheet" type="text/css" ...
<% } %>

This will make intellisense happy, and excludes the stylesheet when the page is rendered, so that it is not included twice.

Robert Harvey
Will try this, thank you. That's a flaw in ASP.NET MVC that hopefully will be fixed in the next version....
Alex
Alex, it's not a fix as it's valid HTML to use classes not in the style sheet. Robert's solution fixes "errors" on classes which are in the CSS, but VS will still give incorrect warnings on those which aren't, even in non-MVC ASP.NET. So I can't agree that it's an MVC "flaw."
Craig Stuntz
"...not a fix..." -> "...not a flaw..."
Craig Stuntz
You're right, its more of a Visual Studio flaw. Blamed the wrong guy :)
Alex
Unfortunately I get "Unreachable code detected" warning...
Konstantin
+2  A: 

This is not a flaw in ASP.Net MVC, and I don't think it's going to be it's going to be fixed in the next version. This is a 'limitation' (notice how i avoid the word flaw) in asp.net (not just mvc) that prevents it from accessing header information that's included in the master page. So you don't have access to javascript/css in the content pages/usercontrols.

The code provided by Robert Harvey is a hack solution that we've been using to overcome this.

It works by using the enclosing the include file in an if block that's always false. So the compiler sees the css file but the runtime doesn't.

Cyril Gupta