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

50 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
  16. I am very thankfull to you for sharing this fantastic article , I appereciate your work .

    mcafee.com/activate , mcafee.com/activate

    ReplyDelete
  17. This antivirus scans all the folders on the system so that any malware cannot damage the users' essential documents. 

    mcafee.com/activate , mcafee.com/activate

    ReplyDelete
  18. It read your article every thing is clear , Thanks for sharing.

    office.com/setup , mcafee.com/activate

    ReplyDelete
  19. Activate Trendmicro - Follow steps to download, install, and activate Trend Micro product. Type the product key by clickingtrendmicro.com/activation.

    ReplyDelete
  20. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. hyip template

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

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

    ReplyDelete
  27. We are tied directly into the sate’s renewal database which allows us to process your request almost instantly. buy essays HD lace

    ReplyDelete
  28. You have written such a interesting blog.it is worth of reading.keep updating and sharing with us

    Digit me
    Zen Watson

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

    ReplyDelete
  30. 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
  31. 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
  32. Cool stuff you have and you keep overhaul every one of us rechargeable LED

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

    ReplyDelete
  35. 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
  36. The contents you mentioned in post are too good and  very useful. I will keep it in mind, thanks for sharing the information keep updating further If you are looking  for  the best antivirus software and support so you should try one of them which may help you a lot. Click below:
      Norton.com/setup 

    ReplyDelete
  37. 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
  38. 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
  39. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.

    hyip templates

    ReplyDelete
  40. 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
  41. We are working as public beneficiaries as we share analyzed and evaluated solutions with people who are looking for a good solution on McAfee login. If you are also looking for such assistance. You can either go to our website or contact us via call.
    Visit here: https://mcafeepro.com/

    ReplyDelete
  42. Plenty Of Fish is a leading social network for dating. It offers the opportunity to meet people of dating age from different countries. This platform not only in a location close to you, but specifically somebody who specializes POF in marriages. Explore fast solutions in
    POF Login
    Plenty Of Fish Login
    Plenty Of Fish Login Page
    Pof Login Inbox

    ReplyDelete