How to assign value to JavaScript variable under MVC3 Razor with ViewBag [duplicate]

巧了我就是萌 提交于 2020-01-13 06:44:33

问题


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!


回答1:


try this

var techIDs = "";
@if (ViewBag.URLParameters != null)
{
   @:techIDs = '@ViewBag.URLParameters';
}



回答2:


Maybe:

<script>
    var techIDs = '@Html.Raw(ViewBag.URLParameters ?? String.Empty)';
</script>



回答3:


I found other approach

 var techIDs = "";

   @if (ViewBag.URLParameters != null)
   {
         <text>techIDs = '@ViewBag.URLParameters' ;</text>
   }


来源:https://stackoverflow.com/questions/11978681/how-to-assign-value-to-javascript-variable-under-mvc3-razor-with-viewbag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!