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.
精彩评论