find a location for property in a new city

Thursday, 21 July 2011

RazorEngine TemplateCompilationException Unable to compile template

When using the RazorEngine for the first time for email templating I ran into a "TemplateCompilationException" with the error message "Unable to compile template. Check Errors list for details." Looking further into the error I found further details "error CS0103: The name 'model' does not exist in the current context."

It turns out that your Razor views are not to be writen in exactly the same way as when running within an ASP.NET web context. I suppose I should have guessed this from the way I was used to declaring the model for the WebFormsViewEngine.

Solution

Instead of using the @model declaration at the the top I should have been using @inherits. An example:

Regular Razor view for web:
@model Models.ContactDetailsViewModel
<!DOCTYPE html>
<html>
 <body>
  <div>
   <p>Hello @Model.FullName,</p>
   <p>We will call you at @Model.CallTime on @Model.PhoneNumber</p>
   <p>Thanks</p>
  </div>
 </body>
</html>

Razor view for use as a template:
@inherits RazorEngine.Templating.TemplateBase<Models.ContactDetailsViewModel>
<!DOCTYPE html>
<html>
 <body>
  <div>
   <p>Hello @Model.FullName,</p>
   <p>We will call you at @Model.CallTime on @Model.PhoneNumber</p>
   <p>Thanks</p>
  </div>
 </body>
</html>

Hope this helps and well done for using such an awesome feature of the RazorEngine!

Follow britishdev on Twitter

12 comments:

  1. Hi

    Thanks for the info, this solved the compile problem, however I no longer get intellisense in VS2010. In the cshtml file the intellisense tells me that a reference is missing to the RazorEngine assembly.

    Did you get this problem?

    ReplyDelete
  2. I added the RazorEngine using NuGet so it got all the references for me and it all works well so I can't say I recognise your issue. I'm using VS2010 SP1 if that helps.

    ReplyDelete
  3. The @model keyword is specific to ASP.NET MVC3's RazorViewEngine. The base parser itself doesn't support it, but it does support adding custom keywords. Visual Studio using @model out of the box to support its Intellisense feature (it can use either @inherits or the terse @model).

    You can manually add support for this in RazorEngine v2 but you'd need to recompile it. I'm hoping to introduce a generic keyword extension mechanism in v3 which should easily enable these scenarios.

    ReplyDelete
  4. Thanks for the info Matthew. But much more importantly thanks for making the Razor Engine!

    ReplyDelete
  5. FYI, @model has been implemented in v3, which I am hoping to push onto GitHub soon :-)

    ReplyDelete
  6. Saved my day. Thank you! Thank you very much!!

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I get a TemplateCompilationException but it is on my model type, not on the model keyword. I get the same exception when I change it to "@inherits RazorEngine.Templating.TemplateBase<MyType>". Any pointers there?

    ReplyDelete
  10. I resolved the issue I was having. I was not fully-qualifying MyType. After fully qualifying it, e.g. "@model ModelNamespace.MyType", it now works. This blog was helpful: http://mehdi.me/generating-html-emails-with-razorengine-basics-generating-your-first-email/

    ReplyDelete