RazorEngineCore icon indicating copy to clipboard operation
RazorEngineCore copied to clipboard

Request: support @model directive

Open sergeyt opened this issue 4 years ago • 15 comments

I've noticed it is not supported yet, right?

sergeyt avatar Apr 16 '20 18:04 sergeyt

@model is controlled by RazorEngineCore itself so you dont need to use this directive and need to remove it from your template.

@using is supported too. Most likely you will need to explicitly link assembly https://github.com/adoconnection/RazorEngineCore/wiki/@Inject-and-referencing-other-assemblies#linking-assemblies (I was unable to parse @using-s and do it programatically in a reasonable time)

adoconnection avatar Apr 17 '20 09:04 adoconnection

To keep intellisense suggestions use @Inherits instead of @model https://github.com/adoconnection/RazorEngineCore/wiki/Switch-from-RazorEngine-cshtml-templates#template

adoconnection avatar Apr 17 '20 09:04 adoconnection

@adoconnection thanks for clarifications. Checked that @using works.

Note @inherites cannot be used for classes without parameterless constructor. So @model would be nice to support (e.g. just ignore it on compile time).

sergeyt avatar Apr 17 '20 10:04 sergeyt

@sergeyt wont it be hidden surprice, if someone will put @model TypeA directive and then figure out it was ignored and replaced by RazorEngineCode?

adoconnection avatar Apr 22 '20 13:04 adoconnection

@adoconnection it can be a documented limitation/behavior. @model is for VS intellisense wheen @inherits is not applicable

sergeyt avatar Apr 22 '20 14:04 sergeyt

@adoconnection, parse the usings is little bit reasonable. Is it possible for me to do some work here? :) @sergeyt, I mention something under https://github.com/adoconnection/RazorEngineCore/discussions/50 may help.

garryxiao avatar May 12 '21 12:05 garryxiao

@garryxiao sure, the only thing is I would like it to be done using Razor tools, sinse razor somehow parse them there should be a way to get usings collection. But I was not able to find out how.

There is always a plan B to use regex or something on a template string, I would like to avoid that.

adoconnection avatar May 12 '21 13:05 adoconnection

Please include this in the wiki, or make the wiki public so I could add it :)

304NotModified avatar Jul 21 '21 12:07 304NotModified

@304NotModified wiki is open now :)

adoconnection avatar Jul 21 '21 20:07 adoconnection

Done! https://github.com/adoconnection/RazorEngineCore/wiki/@model-and-Visual-Studio-IntelliSense

304NotModified avatar Jul 22 '21 08:07 304NotModified

Note @inherites cannot be used for classes without parameterless constructor.

Is this really true? I tried it and don't see the issue? (check IntelliSense, compiling and running?)

it can be a documented limitation/behavior. @model is for VS

I really prefer support for parsing @model and documenting this behavior. Maybe also with a better error message? If we use now @model MyModel, we get this error:

RazorEngineCore.RazorEngineCompilationException
Unable to compile template: j2rpxc2v.45u(7,7): error CS0103: The name 'model' does not exist in the current context
   at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options)
   at RazorEngineCore.RazorEngine.Compile[T](String content, Action`1 builderAction)

Any leads where to start to get this implemented?

304NotModified avatar Jul 22 '21 08:07 304NotModified

I'm having an issue where I'm trying to use the example for @Include and Template, combined with the example for strongly-typed models using @inherit Base<T>.

The problem is that the templating approach shows you should use a MyTemplateBase, but that's not generic, so I can't put @include MyTemplateBase<MyModel> at the top as it doesn't accept T. I tried making it generic, but got stuck here: image

benmccallum avatar Aug 13 '21 13:08 benmccallum

It seems like maybe this wrapper is only required when the model is an anonymous type, based on here: https://github.com/adoconnection/RazorEngineCore/blob/a86a24bab26aadf9059d28c001a8c51898823680/RazorEngineCore/RazorEngineCompiledTemplate.cs#L94-L97

So in my case I can probably just remove that code. Will see how I go

benmccallum avatar Aug 13 '21 14:08 benmccallum

It seems like maybe this wrapper is only required when the model is an anonymous type, based on here: So in my case I can probably just remove that code. Will see how I go

Right, without AnonymousTypeWrapper Razor will not be able to read properties of supplied anonymous object

adoconnection avatar Aug 14 '21 22:08 adoconnection

On the topic: The model is handled by the template, but if it happens @model directive is present, an exception is thrown at compile. Is there a way to ignore this directive and keep on compiling?

seppo498573908457 avatar Sep 21 '23 09:09 seppo498573908457