1

I am struggling for hours to render a view from another controller to a string. I use that to generate email templates. For this I created a controller called EmailController that has a view ConfirmationEmail.cshtml I want to render that view in Home.Index action for example. The reason for this is organisation because I want to have the email views in ~/View/Email/... and EmailController will be used just for rendering them. Also ~/View/Shared/Email/... will be ok if you have another suggestion.

Do you suggest another approach?

I saw this thread ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action but I cannot make it work.

I need something like:

    public ActionResult Index()
        {
                EmailController emailController = new EmailController();
                ControllerContext context = new ControllerContext(this.ControllerContext.RequestContext, emailController);
                EmailController.RenderPartialViewToString(emailController, "ConfirmationEmail", new EmailModel());
}

This does not work :( and the viewResult object in RenderPartialViewToString method is null. Do you have a solution for this?

Community
  • 1
  • 1
Radu D
  • 3,505
  • 9
  • 42
  • 69
  • My I please ask why doing it in other controller? – Emmanuel N Jan 16 '12 at 15:36
  • I want this like an email generation utility ... and I will call it from another controller ... for example after a user register I will generate confirmation email and send it. Not all necessarily in Account.Register action. I added EmailController for better views organisation. Practically ConfirmationEmail is a shared view ... but I don't want it in the Shared folder. – Radu D Jan 16 '12 at 15:41
  • You are using .cshtml files to render an e-mail, because Razor makes it convenient. But now you have a problem with MVC execution pipeline, and resolving it (if ever possible) will be ugly. I'd suggest rendering e-mails in a separate helper method, and calling it both from your EmailController and from your other controllers – Zruty Jan 16 '12 at 15:45
  • Your context variable isn't being used above? – Chris S Jan 16 '12 at 15:46

3 Answers3

1

Did you check out MvcMailer project?

https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
  • This is exactly what I need. Wonderful library. – Radu D Jan 16 '12 at 16:28
  • @RaduD glad to help, I use it in my own application and it's really great, plus it's open source so if something goes wrong you have a chance to fix it and contribute it back :D – Bassam Mehanni Jan 16 '12 at 16:32
0

Why not use FluentEmail? It takes a few seconds to setup using Nuget, and you get all the goodness of Razor templates, with a simple to use email tool.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

EmailController.RenderPartialViewToString you passed in emailController instead of context

Modify that line to something like:

  EmailController.RenderPartialViewToString(context, "ConfirmationEmail", new EmailModel())
Emmanuel N
  • 7,350
  • 2
  • 26
  • 36
  • that does not work ... because the internals use Request.Data in order to find correct View – Radu D Jan 16 '12 at 16:11