Codey
Codey

Reputation: 517

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

Upvotes: 0

Views: 618

Answers (2)

Nasar Eddaoui
Nasar Eddaoui

Reputation: 81

Hi I've had a similar issue before and for a statistical table so try using the code below I'm sure it will work. var val = @html.Raw(ViewBag.Lon);

Upvotes: 0

user786
user786

Reputation: 4374

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

Upvotes: 1

Related Questions