Skip to content

Request: support @model directive #14

Open

Description

@sergeyt

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

Activity

  1. adoconnection commented on Apr 17, 2020

    @adoconnection
    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)

  2. adoconnection commented on Apr 17, 2020

    @adoconnection
    Owner
  3. sergeyt commented on Apr 17, 2020

    @sergeyt
    Author

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

  4. changed the title [-]Request: support @model and @using directives[/-] [+]Request: support @model directive[/+] on Apr 17, 2020
  5. adoconnection commented on Apr 22, 2020

    @adoconnection
    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?

  6. sergeyt commented on Apr 22, 2020

    @sergeyt
    Author

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

  7. garryxiao commented on May 12, 2021

    @garryxiao

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

  8. adoconnection commented on May 12, 2021

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

  9. 304NotModified commented on Jul 21, 2021

    @304NotModified
    Contributor

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

  10. adoconnection commented on Jul 21, 2021

    @adoconnection
    Owner

    @304NotModified wiki is open now :)

  11. 304NotModified commented on Jul 22, 2021

    @304NotModified
    Contributor
  12. 304NotModified commented on Jul 22, 2021

    @304NotModified
    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?

  13. benmccallum commented on Aug 13, 2021

    @benmccallum

    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

  14. benmccallum commented on Aug 13, 2021

    @benmccallum

    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

  15. adoconnection commented on Aug 14, 2021

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

  16. seppo498573908457 commented on Sep 21, 2023

    @seppo498573908457

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions