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

19 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
  11. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan. Shipping worldwide. Stitching option available.

    ReplyDelete
  12. Are you in need of Custom Term Paper Writing Services for your various Custom Term Paper Assignment Services? Do not fret; Legitimate Custom Term Paper Writing Services is here to provide the necessary assistance you require to attain your academic aspirations.

    ReplyDelete
  13. If you want to protect your system online or locally from any unforeseen events   Norton.com/Setup   is is a must have software in your PC or Mac.
    Install your office.com/Setup by downloading now. Microsoft Office applications are a complete package if multiple integrations like Microsoft Office, Microsoft Power Point, Microsoft Excel etc. All of these programs have their own features and speciality and are used in a lot of industries, small organizations and big organizations.
    McAfee.com/Activate Since the world is developing each day with new computerized advances, digital dangers, malware, information, and harming diseases have additionally turned out to be increasingly more progressed with every day. These digital contamination's harm a gadget or documents in different ways. McAfee.com/Activate  follows the concept of refine your system, you don’t need to worry about data loss or system failure because of the malfunctions. McAfee.com/Activate   works finely on every system including android and ios and supports device like, computer, laptops, mobile phones and tablets. McAfee.com/Activate  McAfee.com/Activate

    ReplyDelete
  14. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one. hyip templates

    ReplyDelete
  15. The writer is enthusiastic about purchasing wooden furniture on the web and his exploration about best wooden furniture has brought about the arrangement of this article. hyip templates

    ReplyDelete