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!



Hi
ReplyDeleteThanks 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?
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.
ReplyDeleteThe @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).
ReplyDeleteYou 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.
Thanks for the info Matthew. But much more importantly thanks for making the Razor Engine!
ReplyDeleteFYI, @model has been implemented in v3, which I am hoping to push onto GitHub soon :-)
ReplyDelete