user1108948
user1108948

Reputation:

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

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.

Upvotes: 75

Views: 48267

Answers (2)

Shyju
Shyju

Reputation: 218762

In razor views the @ symbol is the magic character which precedes code.

In your case, use 2 @@ symbols; otherwise razor will think this is a code/expression block.

For example:

@@media print.

Alternatively, you can use the Html.Raw method:

@Html.Raw("@")media print

One example usage is when printing a Twitter handle, which has the @ character in it.

Here is a good msdn link to know more about razor syntax and here is one from Phil Hack.

Upvotes: 20

McGarnagle
McGarnagle

Reputation: 102773

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

@@media print

Upvotes: 180

Related Questions