剃刀@if内&LT;脚本&GT;被拒绝VS11 [英] Razor @if inside <script> is rejected by VS11
问题描述
是下面的标记不正确,或者是在MVC4剃刀引擎中的错误?无论哪种方式,什么是最好的解决方法?
Is the following markup incorrect, or is it a bug in the Razor engine in MVC4? Either way, what's the best workaround?
的全部内容的 View1.cshtml 所示。它编译罚款,但智能感知错误高亮是烦人。的(包括为图像,所以你可以看到如何在语法由VS11拒绝。)的
The entire content of View1.cshtml is shown. It compiles fine, but the Intellisense error highlighting is annoying. (Included as an image so you can see how the syntax is rejected by VS11.)
Hote酒店的 @
语法(而不是&LT;文本&GT;&LT; /文本&GT;
)ISN ŧ要么接受
Hote that the @:
syntax (instead of <text></text>
) isn't accepted either.
推荐答案
这是一个有点棘手剃刀混合的JavaScript。就个人而言,我觉得更容易总是用这两个家伙分开要么剃刀 @function
或剃刀定制 @helper
。
It's a little tricky to mix JavaScript with Razor. Personally, I find it easier to always separate these two guys by using either Razor @function
or Razor custom @helper
.
下面是一个简单的例子:
Here's a simple example:
<script type="text/javascript">
function btn_clicked() {
@generateMyJavaScript()
}
</script>
<a href="javascript:void(0)" onclick='btn_clicked()'>Click on me</a>
@functions {
private MvcHtmlString generateMyJavaScript()
{
string jsBuilder = string.Empty;
if (true)
jsBuilder = "alert('Hey!');";
return MvcHtmlString.Create(jsBuilder);
}
}
这篇关于剃刀@if内&LT;脚本&GT;被拒绝VS11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!