Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:44:02+00:00 2026-05-22T01:44:02+00:00

I’m passing a JSON object to Javascript via the ViewBag with the following code

  • 0

I’m passing a JSON object to Javascript via the ViewBag with the following code in my view:

var jsonResultData = @Html.Raw(ViewBag.JsonResultData);

This approach works fine but VisualStudio keeps giving me a ‘Conditional compilation is turned off’ warning. It seems VS wants quotes around @Html.Raw(ViewBag.JsonResultData); If I add quotes jQuery sees the variable as a string rather than JSON data.

Is my approach flawed? Is there another way I should be approaching this? If not can I disable the VS warning? An annoying side effect of the warning is I can’t format my code using CTRL K-D.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:44:03+00:00Added an answer on May 22, 2026 at 1:44 am

    Why are you using ViewBag? I suppose in your controller action you have manually serialized some model into JSON, haven’t you? Something like this:

    public ActionResult Foo()
    {
        var model = ...
        ViewBag.JsonResultData = new JavaScriptSerializer().Serialize(model);
        return View(model);
    }
    

    I wouldn’t recommend you doing this. Instead do this:

    public ActionResult Foo()
    {
        var model = ...
        return View(model);
    }
    

    and in your view:

    <script type="text/javascript">
        var jsonResultData = @Html.Raw(Json.Encode(Model));
    </script>
    

    As far as the warning is concerned, well, Razor Intellisense is far from perfect. You might indeed get some warnings especially when you mix razor with javascript. We can only hope they will fix this in future versions of ASP.NET MVC. For the moment ignore those warnings. To be honest when I am working with a view I no longer look at warnings or error in Visual Studio as I know in advance that they are buggy and my application works perfectly fine when run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
i got an object with contents of html markup in it, for example: string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I used javascript for loading a picture on my website depending on which small
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.