Asp.Net MVC2 RenderAction changes page mime type?

Posted by Gabe Moothart on Stack Overflow See other posts from Stack Overflow or by Gabe Moothart
Published on 2010-03-15T15:25:44Z Indexed on 2010/03/15 15:49 UTC
Read the original article Hit count: 1014

It appears that calling Html.RenderAction in Asp.Net MVC2 apps can alter the mime type of the page if the child action's type is different than the parent action's.

The code below (testing in MVC2 RTM), which seems sensible to me, will return a result of type application/json when calling Home/Index. Instead of dispylaying the page, the browser will barf and ask you if you want to download it.

My question: Am I missing something? Is this a bug? If so, what's the best workaround?

controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData[ "Message" ] = "Welcome to ASP.NET MVC!";

        return View();
    }

    [ChildActionOnly]
    public JsonResult States()
    {
        string[] states = new[] { "AK", "AL", "AR", "AZ", };

        return Json(states, JsonRequestBehavior.AllowGet);
    }
}

view:

<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<script>
  var states = <% Html.RenderAction("States"); %>;
</script>

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-mvc