Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Created September 27, 2012 08:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/3792842 to your computer and use it in GitHub Desktop.
Save jsakamoto/3792842 to your computer and use it in GitHub Desktop.
return result as PartialViewResult, and translation from PartialViewResult to string.
public ViewResultBase Render(HtmlHelper helper)
{
return new PartialViewResult
{
ViewName = "AnyPartialViewResult",
ViewData = new ViewDataDictionary(helper.ViewData)
{
Model = new AnyPartialViewModel()
},
TempData = helper.ViewContext.TempData
};
}
public static IHtmlString ToHtml(this ViewResultBase result, HtmlHelper html)
{
using (var writer = new StringWriter())
{
var context = html.ViewContext.Controller.ControllerContext;
var viewEngineResult = result
.ViewEngineCollection
.FindPartialView(context, result.ViewName);
result.View = viewEngineResult.View;
var viewContext = new ViewContext(
context,
result.View,
result.ViewData,
result.TempData,
writer);
result.View.Render(viewContext, writer);
viewEngineResult.ViewEngine.ReleaseView(context, result.View);
return new MvcHtmlString(writer.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment