Copied to clipboard

Flag this post as spam?

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


  • Yaco Zaragoza 88 posts 363 karma points
    May 01, 2023 @ 17:59
    Yaco Zaragoza
    0

    How do I check if a richtext area is empty?

    I have a rich text area called "preferredExperience" and I only want to show it if it has data.

    I tried

    @if (@Model.Value("preferredExperience") != null)
    {
    <h1>Preferred Experience</h1>
    <p>@Model.Value("preferredExperience")</p>
    }
    

    and

    @if (!string.IsNullOrWhiteSpace(@Model.Value("preferredExperience")))
    {
        <h1>Preferred Experience</h1>
        <p>@Model.Value("preferredExperience")</p>
    }
    

    but the title (H1) is always showing.. Can someone tell me what is the correct way to check for empty/Null?

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    May 01, 2023 @ 18:40
    Marc Goodson
    100

    Hi Yaco

    Does

    @if (Model.HasValue("preferredExperience")) 
    {
    <h1>Preferred Experience</h1>
    <p>@Model.Value("preferredExperience")</p>
    }
    

    Give what you need?

    Regards

    Marc

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 02, 2023 @ 12:48
    Dave Woestenborghs
    0

    Hi Yaco,

    Can you try this.

        var text = Model.Value<IHtmlEncodedString>("preferredExperience");
    var hasText = text != null && !string.IsNullOrWhiteSpace(text.ToString()));
    
    if(hasText)
    {
    <h1>Preferred Experience</h1>
    @text
    }
    

    Dave

  • Yaco Zaragoza 88 posts 363 karma points
    May 03, 2023 @ 14:04
    Yaco Zaragoza
    0

    I tested @if (Model.HasValue("preferredExperience")) and it worked well.

    Thank you both

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies