FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

Doesn't work with models

Open TDK1964 opened this issue 7 years ago • 4 comments

I am using .NET Core 2 and added the following to my csproj file

    <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
    <PreserveCompilationContext>true</PreserveCompilationContext> 

I have done the following

  1. Created a model
  2. Created a razor template
  3. Populated the template with the model references e.g. @Model.IssueNumber
  4. I pass the model into template like this - email.UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}\\Features\\Shared\\EmailTemplates\\MyTemplate.cshtml", model);

When I run it I get a NullReferenceException: Object reference not set to an instance of an object.

However, if I use a basic file e.g. MyTemplate.cshtml without any references to models etc and add the content as Test ##IssueNumber## the following code works!! email.UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}\\Features\\Shared\\EmailTemplates\\MyTemplate.cshtml", new {IssueNumber = "123"});

Am I doing something wrong?

TDK1964 avatar Jan 26 '18 13:01 TDK1964

Please ensure you adding this line of code

// Using Razor templating package Email.DefaultRenderer = new RazorRenderer();

At your start up. This should work

piusn avatar Feb 14 '18 06:02 piusn

This is related to Razorlight issues (toddams/RazorLight#147) You can use the fix in the linked issue, or wait for the next release of razorlight.

Enslev avatar Apr 17 '18 09:04 Enslev

If anyone is still hitting this, I've added a temporary build of Razorlight with the linked fixes and uploaded a new FluentEmail.Razor to our pre-release myget feed (2.5.0-alpha1): https://www.myget.org/F/fluentemail/api/v3/index.json

bjcull avatar May 29 '18 06:05 bjcull

@bjcull Any solution to this?

epozsh avatar Feb 08 '19 17:02 epozsh