问题
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