Razor/JavaScript 和尾随分号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12111729/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:20:21  来源:igfitidea点击:

Razor/JavaScript and trailing semicolon

javascriptasp.net-mvcrazor

提问by teleball

Using Visual Studio 2012, on a Razor view page, in the JavaScript section, I am getting what I think is a battle between Razor syntax vs JavaScript syntax. In particular, the trailing semicolon in the script section is flagged by intellisense and a compiler warning (not error) is delivered:

使用 Visual Studio 2012,在 Razor 视图页面的 JavaScript 部分,我得到了我认为是 Razor 语法与 JavaScript 语法之间的斗争。特别是,脚本部分的尾随分号被智能感知标记,并提供编译器警告(不是错误):

'Warning 13 Syntax error'.

'警告 13 语法错误'。

If I remove it, then I get a statement termination recommendation (ReSharperin this case, but just good practice).

如果我删除它,那么我会得到一个语句终止建议(在这种情况下是ReSharper,但只是很好的做法)。

<script type="text/javascript">
    $().ready(function(){
        var customer = @Html.Raw(ViewBag.CustomerJSON);  // <- Razor (I think) doesn't like this semicolon
    });
</script>

Is this a bug in Razor? If so, is there a way I can rewrite this to avoid this issue?

这是 Razor 中的错误吗?如果是这样,有没有办法重写它以避免这个问题?

回答by Darin Dimitrov

Is this a bug in Razor?

这是 Razor 中的错误吗?

Absolutely not. Run your application, and it will work as expected.

绝对不。运行您的应用程序,它将按预期工作。

It is a bug in the tools you are using (Visual Studio 2012, ReSharper, ...) that are incapable of recognizing perfectly valid syntax and warning you about something that you shouldn't be warned about. You could try opening an issue on the Microsoft Connect site and signalling this bug if that hasn't already been done.

这是您使用的工具(Visual Studio 2012、ReSharper 等)中的一个错误,它们无法识别完全有效的语法并警告您不应该警告的内容。如果尚未完成,您可以尝试在 Microsoft Connect 站点上打开一个问题并发出此错误信号。

回答by Mirko

Since this still seems to be happening and it is a nuisance I figured I will at least let others know what I ended up using as a "hack". I don't want to ignore the warning and would rather accept a hokier syntax (and yes someone is going to say this will kill performance :))

由于这似乎仍在发生,而且很麻烦,我想我至少会让其他人知道我最终用作“黑客”的内容。我不想忽略警告,而宁愿接受更高级的语法(是的,有人会说这会降低性能:))

What I use as a workaround is to use a client side addition at the end. For me this error occurred on defining an "integer" constant, so

我用作解决方法的是最后使用客户端添加。对我来说,这个错误发生在定义一个“整数”常量时,所以

window.foo = @(Model.Something);

gave me the good old semicolon error. I simply changed this to:

给了我旧的分号错误。我只是将其更改为:

window.foo = @Model.Something + 0;

(In the stated questions case you should just be able to add '', so + ''.

(在上述问题的情况下,您应该只能添加 '',所以 + ''。

I know there is a whole another addition happening on the client and it isn't elegant, but it does avoid the error. So use it or don't, but I prefer this over seeing the warning/error.

我知道客户端上发生了另一个添加,它并不优雅,但它确实避免了错误。所以使用或不使用它,但我更喜欢这个而不是看到警告/错误。

If someone knows of a server-side syntactical workaround for this I would prefer this to the client-side one, so please add.

如果有人知道服务器端的语法解决方法,我更喜欢这个而不是客户端的,所以请添加。

回答by MichaelMitchell

I found that wrapping the Razor syntax in a JavaScript identity function also makes the IDE happy.

我发现将 Razor 语法包装在 JavaScript 标识函数中也能让 IDE 满意。

<script type="text/javascript">
    @* I stands for Identity *@
    function I(obj) { return obj; }
    $().ready(function(){
        var customer = I(@Html.Raw(ViewBag.CustomerJSON));
    });
</script>

回答by BRICHARDSON

This worked for me:

这对我有用:

var customer = @Html.Raw(ViewBag.CustomerJSON + ";")

回答by Serj Sagan

Here's a workaround for booleans:

这是布尔值的解决方法:

var myBool = @(Model.MyBool ? "true;" : "false;")

回答by Greg Ferguson

This worked for me

这对我有用

@Html.Raw(string.Format("var customer = {0};", ViewBag.CustomerJSON));

回答by Nateous

<script type="text/javascript">
    $().ready(function(){
        var customerName = ('@ViewBag.CustomerName');  // <- wrap in parens
    });
</script>

Isn't it as simple as wrapping in parentheses? Putting values through the console seem to work fine with no side effect.

难道不是用括号括起来那么简单吗?通过控制台输入值似乎工作正常,没有副作用。

It works for strings, but it still gives the error for non-quoted values, but I still like this for string values. For numbers you could just use parseInt('@Model.TotalResultCount', 10).

它适用于字符串,但它仍然为非引用值提供错误,但我仍然喜欢字符串值。对于数字,您可以只使用parseInt('@Model.TotalResultCount', 10).