12

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 !

0

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Will try this, thank you. That's a flaw in ASP.NET MVC that hopefully will be fixed in the next version....
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."
"...not a fix..." -> "...not a flaw..."
You're right, its more of a Visual Studio flaw. Blamed the wrong guy :)
6

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.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.