Wednesday, February 12, 2014

Razor SendTemplatedEmail @Html.Raw

I was using a piece of code to send templated emails which the body is contained in a .cshtml file.
Basically the code takes an html template in the chtml file and smooshes it with the model values to create custom email to send - like a custom greeting. I had to also insert a custom closing that may or may not contain html. I attempted to use @Html.Raw(@Model.Closing) but it would throw some unknown exception and then subsequently not send the email. It was quite difficult to debug because it was hard to debug the razor form with how the call was being made.
Anyhow, after several hours, I found I should just use @Raw(@Model.Closing) rather than @Html.Raw(@Model.Closing). This was advice that was given however it was tricky because in Visual Studio the little red squiggly line says that is going to be a problem....but that was in fact not true it worked:
Here is a sample of what was in the .cshmtl file:
  1. @model Junk.Mail.TemplatesModels.EmailModel
  2. <html>
  3. <body>
  4. <p>
  5. Hello @Model.Name
  6. </p>
  7. <p>
  8. Blah blha blah blah
  9. </p>
  10. <p>
  11. Thank you for your attention.
  12. </p>
  13. @Raw(@Model.Closing)
  14. </body>
  15. </html>
Hope that helps.