Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: support @model directive #14

Open
sergeyt opened this issue Apr 16, 2020 · 15 comments
Open

Request: support @model directive #14

sergeyt opened this issue Apr 16, 2020 · 15 comments
Labels
enhancement New feature or request

Comments

@sergeyt
Copy link

sergeyt commented Apr 16, 2020

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

@adoconnection
Copy link
Owner

@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
Copy link
Owner

adoconnection commented Apr 17, 2020

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

@sergeyt
Copy link
Author

sergeyt commented Apr 17, 2020

@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 sergeyt changed the title Request: support @model and @using directives Request: support @model directive Apr 17, 2020
@adoconnection
Copy link
Owner

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

@sergeyt
Copy link
Author

sergeyt commented Apr 22, 2020

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

@adoconnection adoconnection added the enhancement New feature or request label Jul 1, 2020
@garryxiao
Copy link

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

@adoconnection
Copy link
Owner

@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.

@304NotModified
Copy link
Contributor

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

@adoconnection
Copy link
Owner

@304NotModified wiki is open now :)

@304NotModified
Copy link
Contributor

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

@304NotModified
Copy link
Contributor

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?

@benmccallum
Copy link

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.

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
Copy link

It seems like maybe this wrapper is only required when the model is an anonymous type, based on here:

if (model != null && model.IsAnonymous())
{
model = new AnonymousTypeWrapper(model);
}

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

@adoconnection
Copy link
Owner

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

@seppo498573908457
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants