The name 'media' does not exist in the current context

Cssasp.net MvcHtmlasp.net Mvc-3

Css Problem Overview


In an ASP.NET MVC 3 razor view, I have the code:

<!DOCTYPE html>
<html>
<head>
 <style type="text/css">
    @media print
    {
    table { page-break-inside:auto; width: 100%; }
    tr    { page-break-inside:avoid; page-break-after:auto }
    thead { display:table-header-group }
    tfoot { display:table-footer-group }
    }
 </style>
</head>
<body>
<table>

However I got the error:

The name 'media' does not exist in the current context.

Thanks.

Css Solutions


Solution 1 - Css

The @ is a reserved character in Razor. But you can escape it using @@:

@@media print

Solution 2 - Css

In razor views, @ is the magic character which preceeds code.

In your case, use 2 @@. Otherwise razor will thing that it is some code/ expression.

@@media print.

Alternatively you can use Html.Raw method also.

@Html.Raw("@")media print

You can do the same when printing twitter handle name which has the @ in it.

Here is a good msdn link to know more about razor syntax and here is one from phil hack

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1108948View Question on Stackoverflow
Solution 1 - CssMcGarnagleView Answer on Stackoverflow
Solution 2 - CssShyjuView Answer on Stackoverflow