How to assign value to JavaScript variable under M

2020-07-30 03:59发布

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!

3条回答
ゆ 、 Hurt°
2楼-- · 2020-07-30 04:17

try this

var techIDs = "";
@if (ViewBag.URLParameters != null)
{
   @:techIDs = '@ViewBag.URLParameters';
}
查看更多
时光不老,我们不散
3楼-- · 2020-07-30 04:23

I found other approach

 var techIDs = "";

   @if (ViewBag.URLParameters != null)
   {
         <text>techIDs = '@ViewBag.URLParameters' ;</text>
   }
查看更多
Bombasti
4楼-- · 2020-07-30 04:33

Maybe:

<script>
    var techIDs = '@Html.Raw(ViewBag.URLParameters ?? String.Empty)';
</script>
查看更多
登录 后发表回答