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

47 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. Magic ISO Maker Crack containing exact binary copies of the original CDs. Usually, you have to burn an ISO file on a CD to extract its contents, but now you can simply open an ISO file in MagicISO. Magic ISO Maker 2016 Serial Key can make bootable CD/DVD picture document. With Magic ISO, you can prepare the boot data for CD/DVD picture document, and straightforwardly include/ evacuate/ separate boot picture for the CD/DVD picture record. Download Magic ISO Maker Build 281 Full Serial Key 2016 from here.MagicISO Maker Serial Key is an awesome application that can be used for creating and editing disk images. The interface if he application is very catchy and its explorer based layout makes it easy to navigate. In addition to being able to edit the contents of the disc images without decompressing, it can make a disc image from an existing CD/DVD-ROM. Also, it can make bootable image files (file extension. bif). MagicISO Maker Latest Version can mount disc images with the addition of MagicISO Virtual CD/DVD Manager. with Magic ISO Maker you can burn the image files onto CD-R, CD-RW, DVD-R, DVD-RW. You can also compress and decompress image files as well. Magic ISO Maker can handle upto 10GB of DVD images. All in all Magic ISO Maker Keygen is a very useful and can be used for creating the image files as well as for extracting the content from the image files.

    ReplyDelete
  16. 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
  17. 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
  18. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work gadgetdeep pocket

    ReplyDelete
  19. 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
  20. I have checked this link this is really important for the people to get benefit from. free english course online video

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

    ReplyDelete
  22. 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
  23. 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
  24. Cool stuff you have and you keep overhaul every one of us rechargeable LED

    ReplyDelete
  25. 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
  26. I quite like reading an article that can make people think. Also, thanks for allowing for me to comment! Revitalizing

    ReplyDelete
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. JS Removals offer professional home removal services & rubbish clearances for the East Midlands. With hundreds of 5* Reviews book with confidence. moving company

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

    ReplyDelete
  37. 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
  38. 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
  39. Buy shorts for men from ndure at best prices delivered at your doorstep. Get free shipping

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

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

    ReplyDelete
  42. 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
  43. Buy men clothing online in Pakistan from Al-Fatah at best prices delivered at your doorstep. Get free shipping.

    ReplyDelete
  44. I like this post. Are you in need of custom thesis writing help at affordable rates? Let us help you, we offer top-quality writing hel online.

    ReplyDelete