Asp. Net MVC converts PartialView and View into strings

Source: Internet
Author: User

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 ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.