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?