@Model is null when using RazorEngineTemplateBase
See original GitHub issueI’ve run into an issue when attempting to strongly type a template.
My template has @inherits RazorEngineCore.RazorEngineTemplateBase<MyModelType>
at the top, and this makes intellisense work nicely.
The template compiles just fine, but when I run it, I always get “Object not sent to instance of an object” when I reference any property on my model. I know my model is not null. It works fine when render it without using RazorEngineTemplateBase
.
Running a template with @(Model == null)
works, but always renders “True”.
My generic render function:
private static ConcurrentDictionary<int, IRazorEngineCompiledTemplate<IRazorEngineTemplate>> TemplateCache =
new ConcurrentDictionary<int, IRazorEngineCompiledTemplate<IRazorEngineTemplate>>();
public static string Render<TModel>(string template, TModel model, Assembly[] referencedAssemblies = null)
{
int templateHashCode = template.GetHashCode();
var compiledTemplate = TemplateCache.GetOrAdd(templateHashCode, i =>
{
var razorEngine = new RazorEngine();
var compiledTemplate = razorEngine.Compile<RazorEngineTemplateBase<TModel>>(template, builder =>
{
...
});
return compiledTemplate;
});
return compiledTemplate.Run(instance =>
{
instance.Model = model;
});
}
Any help on this would be greatly appreciated. Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Why is my Model object always null on my Razor Page in ...
Now it works perfectly. The Model object is a the valid object with the property value set as expected. (See highlighted text in...
Read more >Model is always null on Razor page - Microsoft Q&A
hello All, I have the following code on my cshtml page: @if (Model!=null && Model.EmergencyInfos!=null && Model.EmergencyInfos.ToList().
Read more >https://source.lunar.exchange/Saphire/quest-reader...
... RazorEngineTemplateBase - { - public new TModel Model { get; set; } ... WhenWritingNull, + WriteIndented = true }; using var fileStream...
Read more >RazorEngineCore.TestComputers 2020.3.4 on NuGet
CodeProject: Building String Razor Template Engine with Bare Hands · Razor syntax reference ... Run<TModel>(TModel model = null) template.
Read more >csharp/adoconnection/RazorEngineCore/ ...
Learn how to use api csharp/adoconnection/RazorEngineCore/RazorEngineCore/RazorEngineTemplateBase.cs.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Its because of
new
keywork I used: https://github.com/adoconnection/RazorEngineCore/blob/master/RazorEngineCore/RazorEngineTemplateBaseT.csThats design issue, I need to think a little.
Cool, however this breaking change for
new
keywork is confusing. I would prefer something like this, withoverride
keyword