asp.net mvc - ViewBag is not written in output for Razor -


viewbag value ignored while running asp.net mvc4 web page.

enter image description here

here source above image. though checking if viewbag.searchresultsjson null or empty, viewbag still isn't written in output.

<script>     @{         htmlstring jsontext = new htmlstring("");         if (!string.isnullorwhitespace(viewbag.searchresultsjson))         {             jsontext = html.raw(viewbag.searchresultsjson);         }     }      $(document).ready(function() {         var json = @jsontext;         app.value('searchresultsjson', json);     }) </script> 

what missing here?

according code, empty jsontext valid scenario won't focus on why variable empty.

the reason error you're not wrapping @jsontext quotes render valid string on javascript side.

you should change

var json = @jsontext; 

by

var json = '@jsontext'; 

Comments