How to make intellisense works with RazorEngine?
Asked Answered
G

3

22

I am trying to configure RazorEngine so that intellisense works on views. I add RazorEngine and Microsoft.AspNet.Mvc using nuget. I create TestView.cshtml and declare @model MyModel but it says The name 'model' does not exist in the current context. I also cannot use intellisense inside the view.

Do I miss any step here? How to enable intellisense in the View?

Girard answered 11/11, 2014 at 10:9 Comment(12)
Can you give an example (I suspect you should be using Model, not model)Isley
are you by chance extending via a class library? just curious.Ean
That is actually the correct syntax for the model declaration. It is subsequently referred to using @Model though. What version of MVC?Crippen
@Crippen I use MVC 5.1.0 and .NET Framework 4.5.Girard
@Anonymous, That error is displayed if you were to use something like @{ var someValue = model.SomeProperty; } in which case it needs to be Model.SomeProperty (capital M)Isley
@StephenMuecke Capital M also does not work. It shows the same error.Girard
In the web.config in your Views folder, does it say 'Version=5.1.0.0' for 'system.web.webPages.razor'Crippen
The various answers here and here might helpIsley
@Crippen No, it doesn't. It points to version 3.1.0.0 in packages. Changing to 5.1.0.0 doesn't work though.Girard
Sounds like an issue related to the mismatch of MVC versions.Crippen
@markpsmith, Razor 3.1.0.0 is correct for MVC 5.1.0.0Isley
@StephenMuecke - apologies, I should have said in the section <system.web.webPages.razor>Crippen
L
37

You can use

@using RazorEngine.Templating
@using Namespace.Of.My.Model
@inherits TemplateBase<MyModel>

on the top of your template.

This works fine on a new Console Application with Visual Studio 2013 (after adding a reference to RazorEngine). The documentation for this is here.

EDIT:

I noticed that this only works when the RazorEngine project is added to the solution and directly referenced. If you use the NuGet package you additionally need to ensure one of the following to make it work:

  1. Your project output path is set to bin\ instead of bin\Debug\ and bin\Release\.
  2. Copy RazorEngine.dll and System.Web.Razor.dll to bin\
Lyra answered 27/12, 2014 at 20:28 Comment(3)
But I need to use @inherits RazorEngine.Templating.TemplateBase. The first using statement does not work for me.Freer
moving the output to the bin folder works, but can someone explain why?Mesosphere
@Mesosphere My assumption is that this is hardcoded in the Visual Studio Designer.Lyra
B
10

I know this question is kind of old. I could not get anything to work, no matter the solution. I have a hack fix that may be palatable to some. I don't like it very much, but it's the most usable thing I've gotten so far.

The trick is to define the "Model" yourself as a variable from the actual Model. I defined it as "TrueModel", but whatever name you can think of that doesn't collide with "model" or "Model" should work. Then just replace all of your instances of "Model" with "TrueModel".

@using Namespace.To.My.Models
@* This line should still look like an error, 
   but we only really care about the intellisense in the rest of the .cshtml file. *@
@{ ModelType TrueModel = (ModelType)Model; }

<div>
@TrueModel.MyProperty is here now.
</div>
<p> @TrueModel.MyOtherProperty is great! </p>

It's not a great solution, but it might be useful.

Brough answered 3/8, 2016 at 0:47 Comment(7)
Hi @Zachary Dow this is not working for me, I had a model under folder Model with name EmailModel, I have a HTML template under Templates with name EmailTemplate. I wrote @{ EmailModel TrueModel = (EmailModel)EmailModel; } and this is not workingHorseshoes
can you help me in thisHorseshoes
@ManojKalluri First "this is not working" isn't telling me much. Second, "Model" is a special property in MVC. So you'll want to change it to look like this: @{ EmailModel TrueModel = (EmailModel)Model; } probably.Brough
thank you, actually this is not MVC project. this is a DLL and this DLL will be referred in a console app. I would like to send emails using razoreengine and I need to get the email body from the cshtmlHorseshoes
@ManojKalluri I'm sorry, You're right. MVC and Razor are so ubiquitous that it's easy to forget the nuance there. Glad it worked for you.Brough
No It's not worked for me. still struggling. I thanked you for your response when I did as you said @{ EmailModel TrueModel = (EmailModel)Model; } I am getting an error that model doesn't exist in current contextHorseshoes
Thanks.. This worked for me.. I just needed to copy the "RazorEngine.dll" and "System.Web.Razor.dll" files into the "bin" directory instead of the "bin\Debug\" and bam!! it worked..Geehan
E
5

Oh, I faced with such problem while adding Razor Engine to my custom dll project. To solve this you have to:

1.correctly setup namespaces in web config file(hope you have it in views folder, if not - copy from MVC project):

 <system.web.webPages.razor>
 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
 <pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />       
    <add namespace="System.Web.Optimization" />
  </namespaces>
</pages>
</system.web.webPages.razor>
...

2.use to build into bin\ path(not any other, you may use copy post-build command to move results to another place)

3.clean solution and remove obj and bin folders, than build

My views' code starts from @model MyModelClass and all works fine

Entoblast answered 27/12, 2014 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.