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

后端 未结 15 3007
挽巷
挽巷 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 05:01

    Quick tip

    For a strongly typed Model just add it to the ViewData.Model property before passing to RenderViewToString. e.g

    this.ViewData.Model = new OrderResultEmailViewModel(order);
    string myString = RenderViewToString(this.ControllerContext, "~/Views/Order/OrderResultEmail.aspx", "~/Views/Shared/Site.Master", this.ViewData, this.TempData);
    

提交回复
热议问题