How to render an ASP.NET MVC view as a string?

后端 未结 15 2921
挽巷
挽巷 2020-11-21 04:40

I want to output two different views (one as a string that will be sent as an email), and the other the page displayed to a user.

Is this possible in ASP.NET MVC bet

15条回答
  •  粉色の甜心
    2020-11-21 04:52

    This article describes how to render a View to a string in different scenarios:

    1. MVC Controller calling another of its own ActionMethods
    2. MVC Controller calling an ActionMethod of another MVC Controller
    3. WebAPI Controller calling an ActionMethod of an MVC Controller

    The solution/code is provided as a class called ViewRenderer. It is part of Rick Stahl's WestwindToolkit at GitHub.

    Usage (3. - WebAPI example):

    string html = ViewRenderer.RenderView("~/Areas/ReportDetail/Views/ReportDetail/Index.cshtml", ReportVM.Create(id));
    

提交回复
热议问题