开发者

Rendering MVC view in code

开发者 https://www.devze.com 2023-03-06 17:38 出处:网络
I\'m trying to render a strongly typed viewpage in code and get the resulting HTML output.Here\'s the code I am using:

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

精彩评论

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