剃刀@if内<脚本>被拒绝VS11 [英] Razor @if inside <script> is rejected by VS11

查看:139
本文介绍了剃刀@if内<脚本>被拒绝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酒店的 @ 语法(而不是<文本>< /文本> )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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆