1. Output View HTML string:
| The code is as follows: |
Copy code |
/// <Summary>
/// Description: outputs the View HTML string.
/// </Summary>
/// <Param name = "controller"> </param>
/// <Param name = "viewName"> View file name </param>
/// <Param name = "masterName"> motherboard page file name </param>
/// <Returns> </returns>
Protected static string RenderViewToString (Controller, string viewName, string masterName)
{
IView view = ViewEngines. Engines. FindView (controller. ControllerContext, viewName, masterName). View;
Using (StringWriter writer = new StringWriter ())
{
ViewContext viewContext = new ViewContext (controller. ControllerContext, view, controller. ViewData, controller. TempData, writer );
ViewContext. View. Render (viewContext, writer );
Return writer. ToString ();
}
} |
2. Output the PartialView HTML string:
| The code is as follows: |
Copy code |
/// <Summary>
/// Description: output the PartialView HTML string
/// </Summary>
/// <Param name = "controller"> </param>
/// <Param name = "partialViewName"> partial view file names </param>
/// <Returns> </returns>
Protected static string RenderPartialViewToString (Controller controller, string partialViewName)
{
IView view = ViewEngines. Engines. FindPartialView (controller. ControllerContext, partialViewName). View;
Using (StringWriter writer = new StringWriter ())
{
ViewContext viewContext = new ViewContext (controller. ControllerContext, view, controller. ViewData, controller. TempData, writer );
ViewContext. View. Render (viewContext, writer );
Return writer. ToString ();
}
} |