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

45 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. 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
  13. 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
  14. Activate Trendmicro - Follow steps to download, install, and activate Trend Micro product. Type the product key by clickingtrendmicro.com/activation.

    ReplyDelete
  15. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. website builder easy free

    ReplyDelete
  16. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. website builder godaddy

    ReplyDelete
  17. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work gadgetdeep pocket

    ReplyDelete
  18. I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us. iphone 11 cases

    ReplyDelete
  19. I have checked this link this is really important for the people to get benefit from. free english course online video

    ReplyDelete
  20. This post is very simple to read and appreciate without leaving any details out. Great work! Short Term Rental Missouri

    ReplyDelete
  21. Mmm.. good to be here in your article or post, whatever, I think I should also work hard for my own website like I see some good and updated working in your site. Registered dietitian

    ReplyDelete
  22. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! LED mini mirror

    ReplyDelete
  23. Cool stuff you have and you keep overhaul every one of us rechargeable LED

    ReplyDelete
  24. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. thomasjefferson

    ReplyDelete
  25. I quite like reading an article that can make people think. Also, thanks for allowing for me to comment! Revitalizing

    ReplyDelete
  26. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging, demeter cologne spray

    ReplyDelete
  27. I’m usually to blogging and i really admire your content. The article has really peaks my interest. I am going to bookmark your web site and keep checking for brand spanking new information. logo designers

    ReplyDelete
  28. You’ll find some fascinating points in time in this post but I do not know if I see all of them center to heart. There’s some validity but I will take hold opinion until I appear into it further. Fantastic post , thanks and we want much more! Added to FeedBurner too new york web designs

    ReplyDelete
  29. Islam, one among largest religions in the world also the one that is misunderstood all time. There are several sources including political, economic & media, framed Islam as misleading religion. And the truth about muslims are always hidden from the light.
    Truth About Islam
    Truth About Muslims
    Islamic Sharia Law
    Women In Islam
    Why Wear Hijab
    Quran And Science
    Muslims Are Not Terrorists
    Polygamy In Islam
    the holy book of islam

    ReplyDelete
  30. Buy online wholesale printed custom lipstick boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!

    ReplyDelete
  31. Thinking pessimistic sides of the things makes us hopeful so that I would like to suggest to all the readers that they must keep an eye on the positive sides of the things for getting success in their life. Assignment Writing Service

    ReplyDelete
  32. Packaging town is a one-stop solution for the short-run cbd tincure boxes needs, and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  33. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Doge

    ReplyDelete
  34. JS Removals offer professional home removal services & rubbish clearances for the East Midlands. With hundreds of 5* Reviews book with confidence. moving company

    ReplyDelete
  35. buy tshirts for men online from dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  36. Thanks for sharing a particularly decent data through this blog. I trust it will help everybody. I like your work, kindly continue to share. We are the main online instructive gateway that gives assignment composing administrations particularly to understudies.
    assignment help

    ReplyDelete
  37. Open the software utility that manages your wireless connections on the computer or wireless device that you want to connect to your router. Using Netgear WNDR4720 Manual, you can setup your router. This utility scans for all wireless networks in your area. Find and select your wireless network name (SSID). The SSID is located on the product label.

    ReplyDelete
  38. Buy shorts for men from ndure at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  39. Buy women cotton tees online from ndure at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  40. Buy grocery online from Al-Fatah at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  41. The given article covers all the important and necessary details for the topic so thanks for sharing this information! it was very informative and helpful!
    myassignmenthelp

    ReplyDelete
  42. Buy men clothing online in Pakistan from Al-Fatah at best prices delivered at your doorstep. Get free shipping.

    ReplyDelete