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>