开发者

Rendering MVC view in code

I'm trying to render a strongly typed viewpage in code and get the resulting HTML output. Here's the code I am using:

public static string RenderPartialToString(ViewPage 开发者_开发百科vp) {
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb)) {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw)) {
            ViewContext vc = new ViewContext();
            vc.ViewData = vp.ViewData;
            vp.RenderView(vc);
        }
    }
    vp.Dispose();
    vp = null;

    string s = sb.ToString();
    sb = null;

    return s;
}

Right now, I am getting a MethodNotImplemented error on the vp.RenderView() line. Is it not possible to render MVC viewpages at this time (MVC 3)?


I would take a look at how they do it in the MvcMailer application which renders MVC views into emails:

https://github.com/smsohan/MvcMailer

http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜