Javascript Razor 中的条件编译已关闭?

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

Conditional Compilation is turned off in Razor?

javascriptasp.net-mvc-3razor

提问by Grahame A

I've got a c# foreach loop that Is outputting some javascript to initialize some progress bars on my razor view.

我有 ac# foreach 循环,它正在输出一些 javascript 来初始化我的剃刀视图上的一些进度条。

@foreach (var item3 in Model)
{
    @:$("#[email protected]").wijprogressbar({ value: @((item3.TotalRedeemed / item3.TotalSold) * 100), fillDirection: "east" });

}

The problem I'm having is visual studio is reporting "Conditional Compilation is Turned Off" on the foreach loop, and the small calculation for value is always coming out as 0, despite TotalRedeemed and TotalSold having values. Am I using the @: operator properly? Thanks for your help.

我遇到的问题是 Visual Studio 在 foreach 循环中报告“条件编译已关闭”,并且尽管 TotalRedeemed 和 TotalSold 具有值,但对值的小计算始终为 0。我是否正确使用 @: 运算符?谢谢你的帮助。

I've tried both suggestions so far and this is what I currently have:

到目前为止,我已经尝试了这两个建议,这就是我目前所拥有的:

 @foreach (var item3 in Model)
 {

     var percentage = (item3.TotalRedeemed / item3.TotalSold) * 100;

     <text>$("#[email protected]").wijprogressbar({ value: @percentage, fillDirection: "east" });</text>

 }

percentage is coming out as 0, but TotalRedeemed and TotalSold have values, as they are printed on the view before this is called. Is there a way to set a break point on my view to see what percentage is before its printed?

百分比显示为 0,但 TotalRedeemed 和 TotalSold 有值,因为它们在调用之前打印在视图上。有没有办法在我的视图上设置断点以查看打印之前的百分比?

采纳答案by Mrchief

Add /*@cc_on @*/in your code.

添加/*@cc_on @*/您的代码。

Update:Found out why they could be 0:

更新:找出原因0

item3.TotalRedeemedand item3.TotalSoldneed to be floator double. If they are int, it comes out to 0.

item3.TotalRedeemed并且item3.TotalSold需要是floatdouble。如果是int,那就出来了0

回答by J R

Timmi4sa - I agree, there isn't much of an answer as to why we are getting this error. I finally got a step closer to understanding it all so I thought i would share.

Timmi4sa - 我同意,对于我们为什么会收到此错误,没有太多答案。我终于更接近理解这一切,所以我想我会分享。

Conditional Compilation is defined my MS as this:

条件编译在我的 MS 中定义为:

Conditional compilation enables JScript to use new language features without sacrificing compatibility with older versions that do not support the features. Some typical uses for conditional compilation include using new features in JScript, embedding debugging support into a script, and tracing code execution.

条件编译使 JScript 能够使用新的语言功能,而不会牺牲与不支持这些功能的旧版本的兼容性。条件编译的一些典型用途包括使用 JScript 中的新功能、将调试支持嵌入到脚本中以及跟踪代码执行。

From what I can tell, we are really talking about a feature of VS. My current guess is this: VS lets you debug JS, but it has to know what the JS is in order to debug it. By default Conditional Compilation is off - I am guessing that there is some additional overhead involved. What we are doing when we are using @Model... in JS is doing exactly what the warning states (more or less) - creating conditional JS. The JS ends up being different things depending on the value of our C#/VB variables.

据我所知,我们真的在谈论 VS 的一个特性。我目前的猜测是:VS 可以让你调试 JS,但它必须知道 JS 是什么才能调试它。默认情况下,条件编译是关闭的 - 我猜这涉及到一些额外的开销。当我们在 JS 中使用 @Model... 时,我们正在做的事情正是警告状态(或多或少) - 创建条件 JS。根据我们的 C#/VB 变量的值,JS 最终会变成不同的东西。

According to MS the solution is to turn Conditional Compilation on as mentioned above via the statement:

根据 MS 的说法,解决方案是通过以下语句打开如上所述的条件编译:

/*@cc_on @*/

While I tend to be a bit anal and prefer to avoid warnings, this may be one I just simply ignore (unless someone can educate me further as to why this is a bad idea).

虽然我倾向于有点肛门并且更喜欢避免警告,但这可能是我只是简单地忽略的(除非有人可以进一步教育我为什么这是一个坏主意)。

If you really want the error to go away and do not like the Conditional Compilation flag, you can wrap the C#/VB code call in double quotes like below. But this feels dirtyand only works because JS is loosely typed... (well with numeric types anyway, strings shouldn't have a problem... regardless, this feels hacky)

如果您真的希望错误消失并且不喜欢条件编译标志,您可以将 C#/VB 代码调用括在双引号中,如下所示。但这感觉很脏,并且只工作因为 JS 是松散类型的......(无论如何,对于数字类型来说,字符串应该没有问题......无论如何,这感觉很hacky)

"@Model.Items.Count()"

Edit: I went and did a little more research... I like the idea of CC even less after skimming this article: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml. I think I will just be ignoring this warning.

编辑:我去做了更多的研究......在浏览了这篇文章后,我更不喜欢 CC 的想法:http: //www.javascriptkit.com/javatutors/conditionalcompile.shtml。我想我会忽略这个警告。

I hope that helps explain away someof the mystery.

我希望这有助于解释一些谜团。

EDIT :

编辑 :

Another option is to throw a HiddenFor down on the form, give it an Id and then populate a JS variable from that field (jQuery makes this pretty easy). This is what I ended up doing for the time being. It eliminates warnings and I often want to compare the JS variable back to the original VMC field anyway. For those of you who need it:

另一种选择是在表单上抛出一个 HiddenFor,给它一个 Id,然后从该字段填充一个 JS 变量(jQuery 使这很容易)。这就是我暂时做的事情。它消除了警告,而且我经常想将 JS 变量与原始 VMC 字段进行比较。对于那些需要它的人:

@* Somewhere in your form - assuming a strongly typed view *@
@Html.HiddenFor(x => x.YourField, new { id = "SomethingMeaningful" })

// and then in your JS
$(document).ready(function(){
  ...
  var jsYourField = $("#SomethingMeaningful").val();
  ...
});

Please note that JS variable and MVC variables do not always 'line up' exactly right so you may need to do some casting or additional work when you copy the variable value into your JS.

请注意,JS 变量和 MVC 变量并不总是完全正确地“对齐”,因此当您将变量值复制到 JS 中时,您可能需要进行一些转换或其他工作。

回答by Darin Dimitrov

Try this:

尝试这个:

@foreach (var item3 in Model)
{
    <text>$("#[email protected]").wijprogressbar({ 
        value: @((item2.TotalRedeemed / item2.TotalSold) * 100), 
        fillDirection: "east" 
    });</text>

}

But a better approach would be to perform this calculation on a view model property, so that your view looks like this:

但更好的方法是在视图模型属性上执行此计算,以便您的视图如下所示:

@foreach (var item in Model)
{
    <text>$('#campaignMeter-@(item.ID)').wijprogressbar({ 
        value: @item.SoldPercentage, 
        fillDirection: "east" 
    });</text>
}

回答by Lismore

I thought I would share what worked for me,

我以为我会分享对我有用的东西,

I had this same issue : System.Web.HttpCompileException (0x80004005)

我有同样的问题:System.Web.HttpCompileException (0x80004005)

below the exception I get: The name 'Url' does not exist

在我得到的异常之下:名称“Url”不存在

When I looked at the View I noticed the only place that was using Url was the razor code @Url.

当我查看视图时,我注意到唯一使用 Url 的地方是剃刀代码 @Url。

This post talked about the references to razor and MVC.

这篇文章讨论了对 razor 和 MVC 的引用。

When i looked in the Views folder in the solution there was a web.config file that contained all the references however it was renamed web.src.config. As soon as I changed it to web.config I was up and running again.

当我查看解决方案中的 Views 文件夹时,有一个包含所有引用的 web.config 文件,但它被重命名为 web.src.config。一旦我将其更改为 web.config,我就可以再次启动并运行。