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:
.cshtml
files with the standard editor (no "store it as a string" or such)@Includes
or @Html.Partial
(Either of which are perfectly acceptable if they work)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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With