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

  • SEARCH
  • Home
  • 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 8652303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:16:42+00:00 2026-06-12T14:16:42+00:00

I have my basic controller and want to return JSON data as a string.

  • 0

I have my basic controller and want to return JSON data as a string. It will NOT be used in AJAX.

In my view I have this:

<script>
    var myJson = @Html.Action("JsonMethod","Controller")
    // Some JS that need JSON data...
</script>

And my controller looks like this

public ActionResult JsonMethod() {
    return Content(Json("Test").ToString());
}

It seems like that I only need to touch the Json object and it will go to HttpContext.Current.Response and change the content type so application/json which will make the view unusable. Am I right about that the Json object requiere a HttpContext, and will by using it, change the content type

  • 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-06-12T14:16:43+00:00Added an answer on June 12, 2026 at 2:16 pm
    Json("Test")
    

    The above is actually a method on the controller, when called it does more than convert the object to JSON, like change the content-type as you found.

    You probably want to use the JavaScriptSerializer directly and place the serialized string in your ViewBag or model.

    You can also just create an extension method to accomplish this directly in the view.

    public static class Helpers
    {
        public static MvcHtmlString GetJson(this HtmlHelper helper, object obj)
        {
            return new MvcHtmlString(new JavaScriptSerializer().Serialize(obj));
        }
    }
    

    And then in the view.

    <script type="text/javascript">
        var myJson = @Html.GetJson(new {test="Test Value"});
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass a JSON data structure to an MVC (3) Controller, have
I want to extend basic controller by my own. This is code of file
I have a basic Spring MVC controller that looks like this: @Controller public void
I have a very basic view controller class, created via interface builder. it only
I have a basic PHP form (a few fields and 2 checkboxes, I want
I have a basic web service that returns the following object as JSON: public
Pretty Basic one here guys. I have a View which holds 2 textfields for
I have setup a basic test app that displays a view containing a label,
I have an several controllers where I want every ActionResult to return the same
I have a Rails controller with a create method, and I want to POST

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.