Created
September 27, 2012 08:17
-
-
Save jsakamoto/3792842 to your computer and use it in GitHub Desktop.
return result as PartialViewResult, and translation from PartialViewResult to string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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