Render Action to String

核能气质少年 提交于 2019-12-06 05:23:23
Rowan Freeman

That helper is useful for rendering a view directly, but not an action. That is, is doesn't even call that action that you want it to call. Instead, that code just finds a view and renders it directly to a writer with the supplied model.

What we want to do is actually call the Email() PartialViewResult and render that.

Observe:

using (var sw = new StringWriter())
{
  PartialViewResult result = Email("Subject", "Body");

  result.View = ViewEngines.Engines.FindPartialView(ControllerContext, "Email").View;

  ViewContext vc = new ViewContext(ControllerContext, result.View, result.ViewData, result.TempData, sw);

  result.View.Render(vc, sw);

  var html = sw.GetStringBuilder().ToString();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!