如何使用 ViewBag 在 MVC3 Razor 下为 JavaScript 变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11978681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:57:06 来源:igfitidea点击:
How to assign value to JavaScript variable under MVC3 Razor with ViewBag
提问by Developer
Possible Duplicate:
How to assign JavaScript value under MVC3
I try to do the following
我尝试执行以下操作
<script type="text/javascript">
var techIDs = "";
@if (ViewBag.URLParameters != null)
{
techIDs = '@ViewBag.URLParameters';
}
</script>
But it seems it's a wrong approach.
但似乎这是一个错误的方法。
Any clue how it could be done?
任何线索如何做到?
Thank you!
谢谢!
回答by Shyju
try this
试试这个
var techIDs = "";
@if (ViewBag.URLParameters != null)
{
@:techIDs = '@ViewBag.URLParameters';
}
回答by Jonathan Prates
Maybe:
或许:
<script>
var techIDs = '@Html.Raw(ViewBag.URLParameters ?? String.Empty)';
</script>
回答by Developer
I found other approach
我找到了其他方法
var techIDs = "";
@if (ViewBag.URLParameters != null)
{
<text>techIDs = '@ViewBag.URLParameters' ;</text>
}