Note that there are some explanatory texts on larger screens.

plurals
  1. POStrongly typed Razor Views as an Email Template engine
    text
    copied!<p>I do know there are several similar questions out there that address this topic, but I am not happy with the answers.</p> <p>What I'd like is to take full advantage of the Razor View engine that can use <em>strongly typed views with layout pages and partial views</em>. I'd like to declare the model at the top of my views and just pass in that model. So simple! No dynamic models that need to be constructed explicitly just before passing them into the view. <strong>EDIT:</strong> I'd like to do this <em>outside</em> of a Web application, inside a class library that can construct email bodies. That's the real issue here is making use of the Razor View Engine that is not a web app.</p> <p>The idea behind this is if I have a <code>User</code> object with 20 properties on it and my first version of the Welcome email only uses the Name, but later I want to edit the template to use their <code>City</code>, <code>State</code> or <code>Email</code> address, then I want to be able to update the Razor view without having to add code and recompile and redeploy. </p> <p>I see <strong><a href="http://razorengine.codeplex.com" rel="nofollow">RazorEngine</a></strong> and I love the idea and simplicity, but everything is a dynamic type. My issue here is that I'll be pulling data as models from the database. By having to shove things into a dynamic type, I don't get all my properties on the View. </p> <p>I also see <strong><a href="https://github.com/smsohan/MvcMailer" rel="nofollow">MvcMailer</a></strong>, which is also nice in theory but it suffers from the same issue which is that all data models passed into the view are dynamic and not strongly typed. </p> <p>I've begun to build out my own version of this, which will require several web namespaces like System.Web.Mvc, System.Web.Razor and System.Web.WebPages - and I'm okay with that. The issue is the lack of HttpContext and ControllerContext and RouteData .... which I'm trying to mock/stub so the. I'm currently trying to investigate DisplayModes and figure out how to mock these outside a real Web context.</p> <p>Is there a better way? If you're going to suggest one of the two previously mentioned frameworks, please note my issues and let me know if there are workarounds.</p> <p><strong>EDIT 2:</strong> After playing a bit more with the RazorEngine, the strongly typed models is not necessarily as much of an issue as I thought. What I'm left wishing for now is the ability to use a Layout page and partial views as well. I can hack around this with string placeholders that get replaced, but probably not very efficient AND not very flexible. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload