RazorGenerator, Templates and @Html

I am trying to use RazorGenerator as an email template mechanism. I want to take a model with data, assemble the correct set of partial views, and return the HTML that I can send via email.
Edit: In addition to this workflow, any solution should be editable as a .cshtml file and be able to compile in dll. For various reasons, it is not practical to deploy the cshtml files themselves - if we cannot embed all of our razor types into a single file, we will not be able to use this offer. Therefore, RazorGenerator.

So far, I have developed every part of it, with the exception of partial ones. When I try to use @ Html.Partial () in the template file, I get: The name 'Html' does not exist in the current context .

Based on this answer , I know that @Html is not part of Razor by default, and there are many answers on how to create an HtmlHelper in a controller. However, I need to create it in a template that does not have a ControllerContext , which I will need to follow these examples.

I also tried using @Include, but the RazorGenerator pattern does not seem to support this. Edit: I also tried to create a new class inherited from TemplateBase<> and copied all the RazorTemplateBase functions, but I get a NullReferenceException in the @Include line.

So my main question is: is there a better way to include another Razor file (with a model) in my file?

Secondly, if there is no better way, how can I create an HtmlHelper?


Edit for generosity: To repeat, the four questions I need in an acceptable answer are the following:

  • Ability to edit .cshtml files with a standard editor (no "store it as a string" or so)
  • The ability to compile everything into one DLL, which can be deployed using our current build system (we cannot deploy many individual .cshtml)
  • The ability to link to one .cshtml file from another and transfer a model equivalent to @Includes or @Html.Partial (any of them is perfectly acceptable if they work)
  • The ability to send the result by e-mail using attachments. (I already have code for this if the result is a string or converted to one.)

Currently, I can get most of the combinations of the three of them, but I canโ€™t get all four at once. I am open to new libraries, replacing RazorGenerator or highlighting any part of what I already have, while the result works as needed.

+6
source share
2 answers

Just a thought, but why donโ€™t you set up other pages, and in your controller code open HTTPWebRequest / WebClient, send the data that is needed there, output all the html / text from this view, combine several calls together and then send all this email 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; } 
+1
source

All Articles