在ASP.NET MVC中写入@Html.Raw(Html.Debug)时出现语法错误
Syntax Error when writing @Html.Raw(Html.Debug) in ASP.NET MVC
我正试图将C#代码与MVC中的javascript结合起来,以便只有在调试模式下使用页面时才能启用"调试"。
这里我得到一个错误:
<script type="text/javascript">
$(document).ready(function () {
Control.init(
"@(ViewBag.Control)",
@Html.Raw(Html.IsDebug() ? "true" : "false")
);
});
</script>
在行中:@Html.Raw(…)我得到"语法错误"
完整代码:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
@Styles.Render("~/Content/css/normalize")
@Styles.Render("~/Content/css/bootstrap")
@Scripts.Render("~/bundles/modernizr")
@RenderSection("head", false)
</head>
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/js/control")
<script type="text/javascript">
$(document).ready(function () {
Control.init(
"@(ViewBag.Control)",
@Html.Raw(Html.IsDebug() ? "true" : "false")
);
});
</script>
@RenderSection("scripts", false)
</body>
</html>
是调试方法:
using System.Web.Mvc;
namespace System.Web.Mvc
{
public static class GUIHelper
{
public static bool IsDebug(this HtmlHelper htmlHelper)
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
}
当我这样做:
"@(Html.IsDebug() ? "true" : "false")"
而不是:
@(Html.IsDebug() ? "true" : "false")
有效,但是…:
<script type="text/javascript">
$(document).ready(function () {
Control.init(
"Control1",
"true" // instead of true
);
});
</script>
*更新:2013-11-27*
解决方案1:
<script type="text/javascript">
$(document).ready(function () {
Control.init(
"@(ViewBag.Control)",
"@(Html.IsDebug() ? "true" : "false")" == "true"
);
});
</script>
解决方案1:
<script type="text/javascript">
$(document).ready(function () {
@Html.Raw("Control.init("+ViewBag.Control+","+(Html.IsDebug() ? "true" : "false")+")")
});
</script>
试试这个?
@{Html.Raw(Html.IsDebug() ? "true" : "false")}
这为我提供了所需的输出:
@(Html.Raw(Html.IsDebug() ? "true" : "false"))
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何设置html元素填充的动画
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何编写HTML输入的JS内联
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量
- 如何使Javascript动态html表及其上的事件
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 如何使用javascript或html下载PDF格式的填写表单
- 视频HTML没有'无法在Internet Explorer 11上工作
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 使用angular重定向到html页面
- 分析高度属性时出现意外值{{specs.height}}.index.html
- 在ASP.NET MVC中写入@Html.Raw(Html.Debug)时出现语法错误
- PHP/HTML:如果 url domain.co.uk/?debug 那么
- 启动时打开Karma debug.html页面