javascript - Call ViewBag, ViewData, TempData in external js file -


in mvc application, want viewbag, viewdata, tempdata values in external .js file. dont want write scripting in view.cshtml, added external js file it. unable call viewbag, viewdata, tempdata values in js file.

can tell me how call these values without writing js code in view.cshtml

because razor code runs on server side, values aren't available javascript runs on client side. way know of getting around declare variables in .js file , initialize them in .cshtml file. perhaps not ideal you, hope helps.

my.cshtml:

... <script type="text/javascript">    myvar = @(viewbag.myvar);  // etc. </script> ... 

my.js

var myvar = null;  function myfunc() {    // check initialization    if (myvar == null)       alert("myvar not initialized!");    ...    // use myvar    ... } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -