javascript - 在 javascript 脚本 block 中评估 Razor @Request.IsLocal

标签 javascript asp.net-mvc razor

我在 javascript block 中使用以下 Razor 语句,但收到“True is undefined”运行时错误。 @Request.IsLocal 呈现为 True 的 bool 值。谢谢。

<script type="text/javascript">

 var test;
 if (@Request.IsLocal  || @Request.IsLocal == 'True')
 {
   test = 'local';
 }
 else
 {
   test = 'not loccal'
 }
 alert(test);
</script>

最佳答案

True 不是 javascript bool 值。这是真实

尝试以下

<script type="text/javascript">

 var test;
 if ("@(Request.IsLocal)" == 'True')
 {
   test = 'local';
 }
 else
 {
   test = 'not loccal'
 }
 alert(test);
</script>

或者

<script type="text/javascript">

 var test;
 if (@Request.IsLocal.ToString().ToLowerInvarient())
 {
   test = 'local';
 }
 else
 {
   test = 'not loccal'
 }
 alert(test);
</script>

关于javascript - 在 javascript 脚本 block 中评估 Razor @Request.IsLocal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41969670/

相关文章:

javascript - jquery 数据表添加行。 https ://datatables. 网

javascript - 在 Google 地理图表中的两点之间绘制曲线

c# - MVC 4 Razor _Layout.cshtml 仅对一页使用 HTML 部分

javascript - jScrollPane 高度根据我的内容刷新(不起作用)

javascript - Qt 5.4 beta WebEngineView调试

c# - 如果字典键为可为空的十进制,则 Mvc SelectList 不绑定(bind)

c# - 在 mvc 站点之间共享代码的 GAC 替代方案

javascript - JSON 不会在 MVC 4 中发布包含 1 个项目的数组

asp.net - html 助手 webgrid 没有显示我分配的样式类

asp.net-mvc - 将缓存对象公开给 View 的最佳方法