Search code examples
javascriptasp.net-mvcviewbag

Access ViewBag from JavaScript


I'm passing some value from the controller to the view with ViewBag, I am trying to access this value from JavaScript like:

<script type="text/javascript">
    window.onload = function () {
     var val = '@(ViewBag.Lon)';
        document.getElementById("demo").innerHTML = val;
    }
</script>

<h3 id="demo" >Passing Data From Controller to View using ViewBag</h3>

Where Lon is propery of type int.

but when I run this code it doest shows the value I'm passing


Solution

  • Razor code can be added with Javascript var x=JSON.Parse(@ViewBag.objJson...)