toddams/RazorLight

How to use method 'IncludeAsync()' to import partial view in root view?

lawtj93 opened this issue · 0 comments

Describe the bug
Hi:
I use the FluentEmail.Razor 3.0.2 in my .NET 6 project and it's working fine for most of what I need it to do.

I have a layout file _DefaultEmailLayout.cshtml that contains a basic header and footer, so in one of my template files ForgotPassword.cshtml i reference this layout file.

@model SharedLibrary.Models.Shared.EmailTemplate

@{
     Layout = Model.LayoutLocation; //contains location of layout file
}

<tr>
    <td class="mceBlockContainer" valign="top" style="background-color:#ffffff; padding-top:21px; padding-bottom:7px; padding-right:25px; padding-left:25px">
        <div class="mceText" id="dataBlockId-40" style="width:100%">
            <p class="last-child">
                <strong>Your password</strong>
            </p>
        </div>
    </td>
</tr>
<tr>
    <td class="mceBlockContainer" valign="top" style="background-color:#ffffff; padding-top:7px; padding-bottom:40px; padding-right:25px; padding-left:25px">
        <div class="mceText" id="dataBlockId-25" style="width:100%">
            <p>Hi @Model.FirstName,</p>
        </div>
    </td>
</tr>
<tr>
    <td class="mceBlockContainer" valign="top" style="background-color:#ffffff; padding-top:21px; padding-bottom:7px; padding-right:25px; padding-left:25px">
        <div class="mceText" id="dataBlockId-40" style="width:100%">
            <p class="last-child">
                <strong>Need Help?</strong>
            </p>
        </div>
    </td>
</tr>
<tr>
    <td class="mceBlockContainer" valign="top" style="background-color:#ffffff; padding-top:7px; padding-bottom:40px; padding-right:25px; padding-left:25px">
        <div class="mceText" id="dataBlockId-25" style="width:100%">
            <p>
            </p>
            <p class="last-child">
                If you need any assistance, our Client Support Team are here to help. Feel free to reach out to our team via email on XXX
            </p>
        </div>
    </td>
</tr>

And in the code itself when I use the FluentEmail.UsingTemplateFromFile().SendAsync() it sends the email successfully and the email is received looking as expected with the layout files header and footer.

What I'm having trouble with is that a few of my emails share a section that you see in the ForgotPassword.cshtml file, the Need Help section, so instead of duplicating it in all those files I placed it in a separate file called _NeedHelpLayout.cshtml

Now if I replace that section in ForgotPassword.cshtml with the below

@{ await IncludeAsync("_NeedHelpLayout.cshtml", Model);}

however when I try to use this line i get the error " CS0103 The name 'IncludeAsync' does not exist in the current context "

Based off reading I found an example of where the include async didn't provide an error and that was when it had these 2 lines at the beginning of the file

@using RazorLight
@inherits TemplatePage<object>

However I have a model that I need to include already that I need for the rest of my templates

@model SharedLibrary.Models.Shared.EmailTemplate

Hopefully I've shared enough details of the problem, if I've missed anything let me know

To Reproduce
In a standard template file, including @{ await IncludeAsync("_NeedHelpLayout.cshtml", Model);}

Expected behavior
No error is shown in VS

Information (please complete the following information):

  • OS: Windows 11 Home 22H2
  • Platform .Net 6
  • RazorLight version : 2.3.1
  • Are you using the OFFICIAL RazorLight package? Yes
  • Visual Studio Community 2022 (64-bit) Version 17.6.5