Manoj Kalluri
Manoj Kalluri

Reputation: 1474

intellisense not working in a cshtml template in a dll that has razorengine reference to get a model properties

HI read some articles about razorengine and I implemented the same.

https://antaris.github.io/RazorEngine/IntellisenseAndResharper.html but it was not working for me.

even researched code from stackoverflow and try to work around it, but it was also not working.

Note: the answer is stackoverflow is not working for me

this is how I tried add intellisense to cshtml

@using RazorEngine
@using Text.Model
@inherits Templating.TemplateBase<EmailModel>

I had a model under folder Model with name EmailModel, I have a HTML template under Templates with name EmailTemplate

my model class

public class EmailModel
{
    public string Timestamp { get; set; }
    public string Name { get; set; }
}

my goal is to write in cshtml

@using RazorEngine
@using Text.Model<!-- namespace-->
@inherits Templating.TemplateBase<EmailModel>

<html>
 <head></head>
  <body>
   <p>
     @Model.Name
     @Model.Timestamp
   </p>
  </body>
</html>

but still my intellisense is showing me squiggly lines @Model

Upvotes: 1

Views: 584

Answers (2)

Manoj Kalluri
Manoj Kalluri

Reputation: 1474

this is dll project so we cannot use @model to bind and get intellisense help in cshtml.

the appropriate solution to this in 2 ways

1) write a class and define a constant in that class

const string EmailTemplate =@"<html>
                                 <p>
                                   @model.Name is going to @model.Country
                                  </p>
                               </html>";

call this constant into the class where you are calling razorengine

and pass this EmailTemplate as template for razorengine and you EmailModel as model

2)write in App.config under argumnets section

<arguments>
    <add name="EmailBody" value="&lt;html&gt;
                                     &lt;body&gt;
                                         &lt;p&gt;
                                             @Model.Name is going to  
                                             @Model.Country*********   
                                         &lt;/p&gt;
                                     &lt;/body&gt;
                                 &lt;/html&gt;" />
<arguments/>

Note: html regular<> tags don't work in app.config

these two ways are working to make razorengine to create email templates in Non MVC (dll & console) applications

Upvotes: 0

Rahul
Rahul

Reputation: 77926

but still my intellisense is showing me squiggly lines @Model

Shouldn't that be since you are referring to @Model as seen in below code segment but I don't see anywhere in your *.cshtml you are having a @Model binded. Basically your view is not a Strongly Typed View. So there is no model and thus the error

   <p>
     @Model.Name
     @Model.Timestamp
   </p>

@inherits is for inheriting a base class as the linked doc in your comment say

you can get minimal intellisense by providing your own base class and using it with the @inherits directive.

You need to add the @Model directive to bind a model like

@model namespace.modelname
<html>
 <head></head>
  <body>
   <p>
     @Model.Name
     @Model.Timestamp
   </p>
  </body>
</html>

For more information see Accessing Your Model's Data from a Controller

Upvotes: 0

Related Questions