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?

4
  • My I please ask why doing it in other controller? Commented Jan 16, 2012 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. Commented Jan 16, 2012 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 Commented Jan 16, 2012 at 15:45
  • Your context variable isn't being used above? Commented Jan 16, 2012 at 15:46

3 Answers 3

1

Did you check out MvcMailer project?

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I need. Wonderful library.
@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
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.

Comments

0

EmailController.RenderPartialViewToString you passed in emailController instead of context

Modify that line to something like:

  EmailController.RenderPartialViewToString(context, "ConfirmationEmail", new EmailModel())

1 Comment

that does not work ... because the internals use Request.Data in order to find correct View

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.