RazorGenerator, Templates, and @Html

asp.net-mvc-3, c#, razorgenerator, template-engine

Solution

Just a thought, but why couldn't you set up other pages and in your Controller Code open up an HTTPWebRequest / WebClient send the data you need there, get all the html/text out of that view, merge several calls together and then email out all that string.

public ActionResult SomeAction() {
  // call other section logic using HttpWebRequest or WebClient
  // /controller/emailsection/{vars}/......
  // Get the string out of the request add it to ViewData["xxx"]
  // rinse and repeat for other sections

}

public ActionResult EmailSection() {
  //put section logic here

  Response.ContentType = "text/html"; // "text/plain"
  Response.Write("Some HttpWebResponse");
  return null;
}

Problem

I'm trying to use RazorGenerator as an email template engine. I want to take a model with the data, assemble the correct set of partial views, and return HTML that I can email out. Edit: In addition to this workflow, any solution would need to be editable as a .cshtml file and be able to be compiled into a dll. For various reasons, it is not practical to deploy the cshtml files themselves - if we can't embed all our razor views into a single file, then we can't use that suggestion. Hence RazorGenerator. So far, I've worked out every part of it, except for the partials. When I try to use @Html.Partial() in a template file, I get: `The name 'Html' does not exist in the current context`. Based on this answer, I know that `@Html` isn't part of Razor by default, and there's many answers out there as to how to create a `HtmlHelper` in a controller. However, I need to create one in a template, which doesn't have the `ControllerContext` that I'd need to follow those examples. I've also tried using @Include, but the RazorGenerator template doesn't seem to support that. Edit: I also tried creating a new class which inherited from `TemplateBase<>` and copied all the functionality of `RazorTemplateBase`, but I get `NullReferenceException`s on the `@Include` line. So, my primary question is: Is there a better way to include another Razor file (with model) into my file? Secondarily, if there isn't a better way, how can I get the HtmlHelper created? Edit for bounty: Just to reiterate, the four things I need in an acceptable answer are: - The ability to edit `.cshtml` files with the standard editor (no "store it as a string" or such) - The ability to compile everything into a single dll which can be deployed using our current build system (we can't deploy lots of individual .cshtml's) - The ability to reference one .cshtml file from another, and pass a model - equivalent to `@Includes` or `@Html.Partial` (Either of which are perfectly acceptable if they work) - The ability to email out the result, with attachments. (I already have code for this, if the result is a string or convertible to one.) I currently can get most combinations of three of these working, but I can't get all four at once. I'm open to new libraries, replacing RazorGenerator, or throwing out any part of what I already have, so long as the result works as needed.

Original source

Related problems