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?

Anonymous
  • 9,366
  • 22
  • 83
  • 133
  • Can you give an example (I suspect you should be using `Model`, not `model`) –  Nov 11 '14 at 10:11
  • are you by chance extending via a class library? just curious. – lemunk Nov 11 '14 at 10:16
  • That is actually the correct syntax for the model declaration. It is subsequently referred to using @Model though. What version of MVC? – markpsmith Nov 11 '14 at 10:17
  • @markpsmith I use MVC 5.1.0 and .NET Framework 4.5. – Anonymous Nov 11 '14 at 10:20
  • @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) –  Nov 11 '14 at 10:27
  • @StephenMuecke Capital M also does not work. It shows the same error. – Anonymous Nov 11 '14 at 10:29
  • In the web.config in your Views folder, does it say 'Version=5.1.0.0' for 'system.web.webPages.razor' – markpsmith Nov 11 '14 at 10:31
  • The various answers [here](http://stackoverflow.com/questions/22832435/mvc-razor-view-intellisense-broken-in-vs-2013) and [here](http://stackoverflow.com/questions/21471887/visual-studio-2013-intellisense-stops-working-for-asp-net-mvc5-controllers) might help –  Nov 11 '14 at 10:35
  • @markpsmith No, it doesn't. It points to version 3.1.0.0 in packages. Changing to 5.1.0.0 doesn't work though. – Anonymous Nov 11 '14 at 10:43
  • Sounds like an issue related to the mismatch of MVC versions. – markpsmith Nov 11 '14 at 10:47
  • @markpsmith, Razor 3.1.0.0 is correct for MVC 5.1.0.0 –  Nov 11 '14 at 11:01
  • @StephenMuecke - apologies, I should have said `in the section ` – markpsmith Nov 11 '14 at 11:05

3 Answers3

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\
matthid
  • 1,644
  • 16
  • 26
  • 6
    But I need to use @inherits RazorEngine.Templating.TemplateBase. The first using statement does not work for me. –  Apr 14 '16 at 08:17
  • 2
    moving the output to the `bin` folder works, but can someone explain why? – jtate Jul 18 '18 at 21:14
  • @jtate My assumption is that this is hardcoded in the Visual Studio Designer. – matthid Jul 28 '18 at 19:49
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.

Zachary Dow
  • 1,897
  • 21
  • 36
  • 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 working – Manoj Kalluri Jun 28 '17 at 17:22
  • can you help me in this – Manoj Kalluri Jun 28 '17 at 21:31
  • @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. – Zachary Dow Jun 28 '17 at 21:42
  • 1
    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 cshtml – Manoj Kalluri Jun 28 '17 at 22:09
  • @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. – Zachary Dow Jun 28 '17 at 22:11
  • 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 context` – Manoj Kalluri Jun 28 '17 at 22:15
  • 1
    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.. – Ocean Airdrop Feb 05 '19 at 16:36
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

FLCL
  • 2,445
  • 2
  • 24
  • 45