在代码中渲染 MVC 视图
我正在尝试在代码中渲染强类型视图页面并获取结果 HTML 输出。这是我正在使用的代码:
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;
}
现在,我在 vp.RenderView() 行上收到 MethodNotImplemented 错误。此时是否无法渲染 MVC 视图页面(MVC 3)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会看看他们如何在 MvcMailer 应用程序中执行此操作,该应用程序将 MVC 视图呈现到电子邮件中:
https://github。 com/smsohan/MvcMailer
http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx
希望这有帮助。
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.