Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Victor Bjørholm 58 posts 180 karma points
    Aug 04, 2020 @ 08:46
    Victor Bjørholm
    0

    Using object data in email template

    Hi Matt,
    Sorry about the big load of questions, but I believe I am nearing the end :D I am now sending a static email and it works fine. I receive the mail, after the order is finished.

    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="utf-8" />
       <title></title>
    </head>
    <body>
       <p>TEST</p>
    </body>
    </html>
    

    I am passing a simple class in the _emailTemplateService.SendEmail(emailTemplate, testClass, "[email protected]", "en-gb");

    The class looks like this:

      class TestClass
        {
            string test = "red";
        }
    

    Sorry to ask such a broad question, but how do I go about using the object data in the email template?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2020 @ 09:25
    Matt Brailsford
    100

    Hi Victor,

    No problem.

    As your view file is a razor file, you can use regular razor syntax. So, in your template at the top you'll want to add a @model directive to tell your view what model type to expect

    @model NameSpace.TestClass
    

    and then within your markup, you can use razors output abilities like the following to render value out

    <h1>The test value is @(Model.test)</h1>
    

    Hope this helps

    /Matt

  • Victor Bjørholm 58 posts 180 karma points
    Aug 04, 2020 @ 10:18
    Victor Bjørholm
    0

    Okay, that makes sense.

    The Namespace in the following snippet, I'd figure would be the NameSpace where the TestClass is defined? Is yes how do I go about it, when the TestClass is defined in my Core Library While the template is in the main project. What would be the best course of action to refer to it?

    @model NameSpace.TestClass
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2020 @ 10:25
    Matt Brailsford
    0

    Hi Victor,

    That is correct, and again, as per regular .NET practice as long as the main project has the core DLL in it's bin folder and your TestClass is public, it should pick it up.

    /Matt

  • Victor Bjørholm 58 posts 180 karma points
    Aug 04, 2020 @ 13:38
    Victor Bjørholm
    0

    Hi Matt,

    I am trying to setup the model as shown by you earlier like this

    @model  FridayHome.core.ClassTest;
    

    However I keep getting this error in Visual Studio:

    ClassTest is a type, which is not valid in the given context
    Only Assignment, call, increment, decrement, await and new object  expressions can be used as a statement
    

    I also get an XML error when the event actually gets triggered...

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2020 @ 13:50
    Matt Brailsford
    0

    What's the markup to your whole template? Sounds like it may be malformed.

    Matt

  • Victor Bjørholm 58 posts 180 karma points
    Aug 04, 2020 @ 13:54
    Victor Bjørholm
    0
    @using FridayHome.core.Services;
    @model  FridayHome.core.ClassTest;
    <html lang="en">
    ClassTest is a type, which is not valid in the given context
    Only Assignment, call, increment, decrement, await and new object expressions can be used as a statement
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h1>The test value is @(Model.test)</h1>
    </body>
    </html>
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2020 @ 13:57
    Matt Brailsford
    0

    Can you try it without the ; on the end of your @model statement?

  • Victor Bjørholm 58 posts 180 karma points
    Aug 04, 2020 @ 14:22
    Victor Bjørholm
    0

    That was it!!

    Thanks a lot, my mailevent seems to be working just fine now! :D

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2020 @ 14:38
    Matt Brailsford
    0

    Great stuff 👍

Please Sign in or register to post replies

Write your reply to:

Draft