Search code examples
c#.netasp.net-mvcrazorrazorengine

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


Solution

  • 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