渲染视图编程为一个字符串 [英] Render View programmatically into a string

查看:85
本文介绍了渲染视图编程为一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的HTML code视图将一个字符串中生成,修改它在我的控制器,然后将其添加到我的JsonResult。

我发现code,它会做什么,我说的是从部分。我想从一个aspx查看虽然做到这一点。

- 额外的解释:

让我们说我有一个页面Fra​​me.aspx的/控制器/帧将返回

我想获得我的手在响应之前出来这样我就可以用JSONP包裹。
我不希望编辑code,每次返回的结果,这就是为什么我要以编程方式加载的看法。

/控制器/帧返回当前Frame.aspx内容:< HTML><身体GT;你好< /身体GT;< / HTML>

假设有呈现一个视图中的字符串生成器功能

  StringBuilder的SB =新的StringBuilder();
的RenderView(SB,框架);

现在采取某人与JSONP包起来:

 公共JsonResult帧(字符串回调)
{
     StringBuilder的SB =新的StringBuilder();
     的RenderView(SB,框架);     返回新JsonResult
     {
         数据=(功能(){+ +回调(+ clientResponse +);})();
         ,
         JsonRequestBehavior = JsonRequestBehavior.AllowGet
     };
}


解决方案

这就像一个魅力(得到它通过SO)。

我用这样的:

 公共类OfferController:控制器
{
    [HttpPost]
    公共JsonResult EditForm(INT标识)
    {
        VAR模型= Mapper.Map<提供,OfferEditModel>(_ repo.GetOffer(ID));        返回JSON(新{状态=OK,部分= this.RenderPartialViewToString(编辑,型号)});
    }
}公共静态部分类ControllerExtensions
{
    公共静态字符串RenderPartialViewToString(这ControllerBase控制器,串partialPath,对象模型)
    {
        如果(string.IsNullOrEmpty(partialPath))
            partialPath = controller.ControllerContext.RouteData.GetRequiredString(行动);        controller.ViewData.Model =模型;        使用(StringWriter的SW =新的StringWriter())
        {
            ViewEngineResult的ViewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext,partialPath);
            ViewContext viewContext =新ViewContext(controller.ControllerContext,viewResult.View,controller.ViewData,controller.TempData,SW);
            //复制模型状态的项目到HTML辅助
            的foreach(在viewContext.Controller.ViewData.ModelState VAR项)
                如果(!viewContext.ViewData.ModelState.Keys.Contains(item.Key))
                {
                    viewContext.ViewData.ModelState.Add(项目);
                }
            viewResult.View.Render(viewContext,SW);            返回sw.GetStringBuilder()的ToString()。
        }
    }
}

I would like to get the html code a view would generate in a string, modify it in my controller, then add it to my JsonResult.

I found code that would do what i'm talking about from a partial. I would like to do it from an aspx View though.

-- Extra explanation:

Let's say I have a page Frame.aspx that /Controller/Frame will return

I would like to get my hand on the response before it out so I can to wrap it with jsonp. I do not wish to edit the return result in code every time, this is why I want to load the view programmatically.

/Controller/Frame currently returns Frame.aspx's content: <html><body>hello</body></html>

Let's say there's a function that renders a view in a string builder

StringBuilder sb = new StringBuilder();
RenderView(sb, "Frame");

now take sb and wrap it with jsonp:

public JsonResult Frame(string callback)
{
     StringBuilder sb = new StringBuilder();
     RenderView(sb, "Frame");

     return new JsonResult
     {
         Data = "(function() { " + callback + "(" +  clientResponse + "); })();"
         ,
         JsonRequestBehavior = JsonRequestBehavior.AllowGet
     };
}

解决方案

This works like a charm (got it through SO).

I use it like this:

public class OfferController : Controller
{
    [HttpPost]
    public JsonResult EditForm(int Id)
    {
        var model = Mapper.Map<Offer, OfferEditModel>(_repo.GetOffer(Id));

        return Json(new { status = "ok", partial = this.RenderPartialViewToString("Edit", model) });
    }
}



public static partial class ControllerExtensions
{
    public static string RenderPartialViewToString(this ControllerBase controller, string partialPath, object model)
    {
        if (string.IsNullOrEmpty(partialPath))
            partialPath = controller.ControllerContext.RouteData.GetRequiredString("action");

        controller.ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialPath);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            // copy model state items to the html helper 
            foreach (var item in viewContext.Controller.ViewData.ModelState)
                if (!viewContext.ViewData.ModelState.Keys.Contains(item.Key))
                {
                    viewContext.ViewData.ModelState.Add(item);
                }


            viewResult.View.Render(viewContext, sw);

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

这篇关于渲染视图编程为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆