You can do this way, providing Json or Any other variable:
1) For exemple, in the controller, you can use Json.NET
to provide Json
to the ViewBag
:
ViewBag.Number = 10;
ViewBag.FooObj = JsonConvert.SerializeObject(new Foo { Text = "Im a foo." });
2) In the View
, put the script like this at the bottom of the page.
<script type="text/javascript">
var number = parseInt(@ViewBag.Number); //Accessing the number from the ViewBag
alert("Number is: " + number);
var model = @Html.Raw(@ViewBag.FooObj); //Accessing the Json Object from ViewBag
alert("Text is: " + model.Text);
</script>