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
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.
And then in the view.