Friday, February 24, 2012

Json Data Islands in ASP.NET MVC

Once you have your island setup as always like:
<div id="dataIsland_in" style="display: none">
    @{
      Html.RenderAction("GetModelIsland", new { variantId = Model.BidVariant.Id });
    }
</div>
You might be surprised with your browser presenting you with “raw html” instead of that rendered page.
This is because with no precaution returning JsonResult would have an implication of content type to be set to “application/json”. To prevent this, just reset the content type to the one you need:
var result = Json(model, JsonRequestBehavior.AllowGet);
 
result.ContentType = "text/html";
 
return result;

No comments: