toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.5k stars 259 forks source link

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

Open lawtj93 opened 11 months ago

lawtj93 commented 11 months ago

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):