Closed dalenewman closed 4 years ago
I might be wrong, but as I can see @model
is not exactly a part of RazorEngine, it is ASP.NET that makes RazorEngine to know of this directive. I actually like this behavior. It let templates be flexible and use any keywords as directives @anything
that are directly tied to TemplateBase, not only to Model.
Have a look: If I use this template:
@model string
Hello @Model
RazorEngineCore (me) adds two directives:
@inherits RazorEngineCore.RazorEngineTemplateBase
@using System.Linq
@model string
Hello @Model
And then RazorEngine (Microsoft) will turn it in following code:
Notice how @model string
turned into Write and WriteLiteral
#pragma checksum "vadrkp0x.pba" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7292c67fd2ce79b6a8a3b20c73e811dee742b3ad"
// <auto-generated/>
#pragma warning disable 1591
namespace TemplateNamespace
{
using System.Linq;
public class Template : RazorEngineCore.RazorEngineTemplateBase
{
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("\r\n");
Write(model); // HERE - default RazorEngine instance thought @model is a variable name
WriteLiteral(" string\r\nHello ");
Write(Model);
WriteLiteral("\r\n");
}
}
}
Why it is so important for you to have @model directive in template? I only see it is useful as a reference for devs
I only find including the @model directive useful because it lets my editor (Visual Studio 2019) know the type and subsequently offer intellisense. What I will do is leave it in my templates (for editing purposes) but remove it before processing it with your library.
Thanks so much for your response and your open source work!
you welcome
@dalenewman this is how you can use IntelliSense in your templates: https://github.com/adoconnection/RazorEngineCore/issues/13
It might just be me, but I get an error saying "The name 'model' does not exist in the current context" whenever I have a @model directive in my strongly typed template. It runs without it, but I'd rather have it there than not. I've been through the gamut trying fixes outlined in this and this stack overflow post.