Forked from cassioeskelsen/RenderViewToString.cs
Last active
December 16, 2015 10:29
-
-
Save zihotki/5420897 to your computer and use it in GitHub Desktop.
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
/* | |
* Original Sample: http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/ | |
*/ | |
public class HomeController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
string MyModelData = ""; | |
var teste = RenderPrintViewToString("About", MyModelData); | |
return View(); | |
} | |
public ActionResult About() | |
{ | |
return View(); | |
} | |
protected string RenderPrintViewToString(string viewName, object model) | |
{ | |
if (string.IsNullOrEmpty(viewName)) | |
viewName = ControllerContext.RouteData.GetRequiredString("action"); | |
ViewData.Model = model; | |
using (StringWriter sw = new StringWriter()) | |
{ | |
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, "PrintLayout"); | |
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); | |
viewResult.View.Render(viewContext, sw); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A code sample to print some asp.net mvc view into a string using print layout