Copied to clipboard

Flag this post as spam?

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


  • Jeroen Roscam 13 posts 113 karma points
    Sep 20, 2018 @ 08:20
    Jeroen Roscam
    0

    Razor email-template: if-statement

    Hello everyone,

    I am trying to edit the email Example-Template.cstml in \Views\Partials\Forms\Emails

    I want to include an if-statement: only show field B if the value of field A = X.

    (Disclaimer: I have never worked with Razor before; I am working only with what I see here.)

    The code I included is:

        <h2>Type of change:</h2>
    @Model.GetValue("mutation")
    
    @if (Model.GetValue("mutation") == "e-mailadress") 
    {
        @Model.GetValue("old-email")
    }
    

    But this doesn't work. The email does get sent. And in the email, it does show the value for "mutation":

    Type of change: e-mailadress

    But it ignores the if-statement.

    I tried simplifying it to test if the if-statement worked at all:

        <h2>Type of change:</h2>
    @Model.GetValue("mutation")
    
    @if (Model.GetValue("mutation") == "e-mailadress") 
    {
        <p>Hello!</p>
    }
    

    But it also ignores this. It seems the if-statement just doesn't work. Any ideas what I am doing wrong? I would think it should be possible to include or exclude fields with an if-statment.

    Thanks,

    Jeroen

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 20, 2018 @ 09:49
    Dave Woestenborghs
    100

    Hi Jeroen,

    Can you try this

    @if (Model.GetValue("mutation").ToString() == "e-mailadress") 
    

    Maybe Model.GetValue does not return a string type but object. When rendering in razor it will do the .ToString() under the hood I think

    Dave

  • warlordone 1 post 71 karma points
    May 06, 2021 @ 19:15
  • Jeroen Roscam 13 posts 113 karma points
    Sep 20, 2018 @ 10:05
    Jeroen Roscam
    0

    Hi Dave,

    This works! Great, thanks.

    I had been struggling with this for days. Great forum, this.

    Thank, Jeroen

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 20, 2018 @ 11:20
    Dave Woestenborghs
    0

    Hi Jeroen;

    Glad to see the issue is solved. Don't forget to mark this topic as solved.

    Dave

  • Simeon Ostberg 123 posts 389 karma points
    Mar 12, 2021 @ 09:48
    Simeon Ostberg
    0

    Hi everyone,

    Please allow me to reply to this old thread. Unfortunately Dave's version doesn't work for me.

    Now I would like to know, if this is an issue connected to the reply? My code would be:

    @if (Model.GetValue("ibanswift").ToString() == "Yes / Ja")
    {
    (do something)
    }
    

    Do I have to do something about the spaces within the answer?

    Thanks!

    Simeon

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 12, 2021 @ 15:41
    Dave Woestenborghs
    0

    Hi Simeon,

    What type of property editor are you using for "ibanswift" property ?

    Dave

  • Simeon Ostberg 123 posts 389 karma points
    Mar 12, 2021 @ 15:49
    Simeon Ostberg
    0

    Hi Dave,

    It's a single choice radio button (if you mean this).

    Thanks! Simeon

  • Simeon Ostberg 123 posts 389 karma points
    Mar 24, 2021 @ 15:54
    Simeon Ostberg
    0

    Hi everyone,

    I also tried this:

    @{
    string iban = Model.GetValue("ibanswift").ToString();
    if (iban == "Yes / Ja")
    {
    (do something)
    }
    

    But this also didn't work. Error message is "error CS1525: Invalid expression term '>'"

    Thanks, Simeon

  • Simeon Ostberg 123 posts 389 karma points
    Sep 16, 2021 @ 15:57
    Simeon Ostberg
    0

    Hi Dave,

    Please allow me to come back to this topic... I would be happy to get any hint...

    Thanks!

    Best, Simeon

  • Simeon Ostberg 123 posts 389 karma points
    Sep 17, 2021 @ 11:20
    Simeon Ostberg
    0

    Hi everyone,

    I managed to solve this. This worked for me:

    @if(@Model.DynamicFields.project.Contains("STC"))
    {
    (do something)
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft