Html 当前上下文中不存在名称“媒体”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13612413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name 'media' does not exist in the current context
提问by McGarnagle
In an ASP.NET MVC 3 razor view, I have the code:
在 ASP.NET MVC 3 razor 视图中,我有代码:
<!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.
谢谢。
回答by McGarnagle
The @
is a reserved character in Razor. But you can escape it using @@
:
该@
是剃刀保留字符。但是你可以使用@@
以下方法来逃避它:
@@media print
回答by Shyju
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.
在您的情况下,请使用 2 @@
。否则剃刀会认为它是一些代码/表达式。
@@media print.
Alternatively you can use Html.Raw
method also.
或者,您也可以使用Html.Raw
方法。
@Html.Raw("@")media print
You can do the same when printing twitter handle name which has the @
in it.
在打印其中包含 twitter 句柄名称时,您可以执行相同的操作@
。
Hereis a good msdn link to know more about razor syntax and hereis one from phil hack