Converts a view to a string.

Source: Internet
Author: User

Converts a view to a string.

To convert some views to html strings, you can refer to the following method:

 

Public static string RenderPartialViewToString (Controller controller, string viewName, object model) {controller. viewData. model = model; try {using (StringWriter sw = new StringWriter () {ViewEngineResult viewResult = ViewEngines. engines. findPartialView (controller. controllerContext, viewName); ViewContext viewContext = new ViewContext (controller. controllerContext, viewResult. view, controller. viewData, controller. tempData, sw); viewResult. view. render (viewContext, sw); return sw. getStringBuilder (). toString () ;}} catch (Exception ex) {return ex. toString () ;}} Source Code

 

You can create a Partial view in ASP. net mvc directly in the corresponding directory. You do not need to create a view Action in the controller.

 

Then, you can load this part of the View:

 

View running

 

Insus. NET prefers some views, because you can create many sides, but you do not need to create relative actions in the controller.

 

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.