I've got the following controller:
public class HelloController
{
public ActionResult Index()
{
return View()
}
public ActionResult Hello()
{
return Json(new{ greeting = "hello, world!" }, JsonRequestBehavior.AllowGet);
}
}
Then, inside Index.cshtml
:
...html stuffs
<script type="text/javascript">
alert("@Html.Action("Hello")");
</script>
What I'm finding is that, when going to this url in my browser, the response content type is application/json; charset=utf-8
which causes the browser to render the html as a string instead of as... a web page.
What's the best way to get around this?
The reason to this is that all
Html.Action
invocations are executed directly. Something like:You got two options:
alert
.Option 1
Option 2
Side note
Internet Explorer is quite aggressive when it comes to caching. The JSON responses will be changed. I therefore recommend that you also specify no cache for the JSON action: