Question

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>
Was it helpful?

Solution

It's not a bug. The JsonResult type is supposed to set the result to JSON, because that's usually what you want.

You don't really want a JSON result here, you want a JSON string. So why not just write that?

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

    return new JavaScriptSerializer().Serialize(states);
}

OTHER TIPS

I Consider this a bug. If this is a child action being rendered, why it would change the parent action response? The same happens with Html.Action, which renders it into a string. My workaround is doing:

Html.ViewContext.HttpContext.Response.ContentType = "text/html";

after calling Html.Action. I suppose someone could write a wrapper Html Helper extension, something like:

var aux = Html.ViewContext.HttpContext.Response.ContentType;
Html.Action(....); // or Html.RenderAction(...)
Html.ViewContext.HttpContext.Response.ContentType = aux;

You're not missing something (unless I am too) and I think this is a bug. I have the same issue in ASP.NET MVC3.

We have a controller action which returns content from a simple content managment system. The CMS allows the user to define the content type of what is returned (for example text/plain or text/xml).

The controller action is either called directly, or called as a child action to allow a view to contain content managed elements.

If a piece of content is created with a content type of "text/plain", and this is embedded on an ASP.NET MVC view, the content type of the parent is overridden and the browser displays HTML.

Gabe, I think you've hit the nail on the head in that there does not appear to be a scenario where the child action overriding the parent is a desirable outcome.

My solution is to branch on ControllerContext.IsChildAction and construct my own return object, but this in my opinion is something that should be handled by the framework.

I'm sure you're aware of this, but in your case I would suggest explicitly setting JsonResult.ContentType to the content type of the parent.

This can be solved by explicitly forcing the mime type "back" to text/html:

return Json(states, "text/html", JsonRequestBehavior.AllowGet);

It doesn't seem like this should be necessary, though.

Like Craig Stuntz said the content type is supposed to change.

A better approach would be calling that action with AJAX and then assigning the returned object to the states variable in the JavaScript code.

I had the problem today. The reason was I need to reuse an existing child action to populate some json data on the page so that unnecessary ajax requests can be avoided.

Based on Jamie and Niv's idea, I created following helper method.

public static MvcHtmlString ChildAction( this HtmlHelper htmlHelper, ActionResult result )
{
   var aux = htmlHelper.ViewContext.HttpContext.Response.ContentType;
   var actionResult = htmlHelper.Action( result );
   htmlHelper.ViewContext.HttpContext.Response.ContentType = aux;
   return actionResult;
}

Call Html.ChildAction instead of Html.Action when you need to use result of child action that returns json data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top