开发者

Return plain HTML from partial view

How can I return HTML in 2 or more JSON fields like this?

retu开发者_StackOverflow社区rn Json(
new 
{
   Breadcrumbs = PartialView("Breadcrumbs/Breadcrumbs", model.Breadcrumbs),
   FolderDetail = PartialView("Detail/Folder", model.FolderDetail)
});

In this case I don't get HTML, but JSON objects, in result.BreadCrumbs and result.FolderDetail


PartialView is an ActionResult that tells the system to render the partial view rather than the output HTML. Here's the code I'm currently using to render views to a string before adding it to a JSON result:

    public static string RenderPartialToString(this Controller controller, string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

        controller.ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜