adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
565 stars 84 forks source link

How to use partial templates like a files? #122

Open VladShupik opened 1 year ago

VladShupik commented 1 year ago

Good evening! I would like to share my problem. I am using your library, and I have one issue with partial views. So, I have 2 files .cshtml - 1) Main General file with . 2) Block

with some text from model.

I implemented custom MyBaseTemplateClass with Extension for Compile method as same as you share in your documentation. I saw Intellisence for Include methods etc. But when I am calling method compile my code crushed and the reason : ---> System.Reflection.AmbiguousMatchException: Ambiguous match found." at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) at System.Type.GetProperty(String name, BindingFlags bindingAttr). COMPILE INCLUDE

Also I attached my source files, second one when I use Include method and first where I create dictionary with 1 partial view and send to compile with main template file, may be you can help me and will say where I have mistake. Thanks for your time, have a nice day! P.S Before compile I read text from .cshtml and return string of template.

VladShupik commented 1 year ago

@adoconnection, I found how to solve the problem that I wrote earlier. If we are used some classes with inheritance and classes have the same names of properties engine compile crushed. You have AnonymousTypeWrapper and method TryGetMember, on 24 line all broke with exception : "Ambiguous match found". So also I found link with the same problem. GetProperty reflection results(https://stackoverflow.com/questions/11443707/getproperty-reflection-results-in-ambiguous-match-found-on-new-property).

Problem

This is idea how can fix it -> PropertyInfo property = this.model.GetType().GetProperties().First(p => p.Name == binder.Name);