【问题标题】:Conditional Compilation is Turned Off VS 2012 razor javascript条件编译已关闭 VS 2012 razor javascript
【发布时间】:2012-08-25 11:00:56
【问题描述】:

我已经搜索过 SO 并没有找到解决此问题的方法。我有这样的代码:

<script>
$("AddToFavorites").Click(function() {
    var apiLink = "/url/AddToFavorites?id=" + @Model.RecipeId;
    $.ajax({
        url: apiLink,
        type: "GET"
    });
});
</script>

.在 Model.RecipeId 中带下划线,我得到一个编译器错误:

Conditional compilation is turned off

难道不能在 javscript 块中使用 c# 代码吗?如果是这种情况,在这种情况下如何绕过它以使 url 动态化?

谢谢。

【问题讨论】:

  • Model.RecipeId 放在单引号之间!

标签: c# javascript razor visual-studio-2012


【解决方案1】:

尝试将代码更改为

<script type="text/javascript">
$("AddToFavorites").click(function() {
    var apiLink = "/url/AddToFavorites?id=" + "@(Model.MerchantID)";
    $.ajax({
        url: apiLink,
        type: "GET"
    });
});
</script>

你也可以添加

/*@cc_on @*/

如果错误继续发生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多