Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Sep 11, 2013 @ 09:15
    Anthony Candaele
    0

    checking for empty property in v6 Razor

    Hi,

    I'm working on my first Umbraco MVC project, so it's all a bit new to me.

    In my Partial View Macro, I try to check if a value for a property is set like this:

    @if(!Model.Content.IsNull("memberEmail"))

                        {

                          <li>

                            <i class="icon-envelope-alt"></i>

                            <a href="mailto:@Model.Content.GetPropertyValue("memberEmail")">@Model.Content.GetPropertyValue("memberEmail")</a>

                          </li>

                        }

     

    But this is not working, I'm getting an invalid cast error.

    I checked the v6 Razor Cheatsheet and it gives this method for property checking:

    .IsNull(string PropertyAlias)

    So I'm not sure why the above code is not working.

    Thanks for your help,

    Anthony

     

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Sep 11, 2013 @ 09:19
    Jeavon Leopold
    100

    Hi Anthony,

    You should use HasValue, e.g

    @if(Model.Content.HasValue("memberEmail"))
    

    Thanks,

    Jeavon

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 11, 2013 @ 09:38
    Anthony Candaele
    0

    Hi Jeavon,

    Thanks a lot, that's pretty simple. I previously tried the HasPropery() method, but that hadn't the result I was looking for, as that method only checks if a doctype has a the given property, not if it's empty.

    greetings,

    Anthony

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Sep 11, 2013 @ 17:13
    Jeavon Leopold
    0

    Hi Anthony,

    No worries, if you want to be really safe you can do

    @if(Model.Content.HasProperty("memberEmail") && Model.Content.HasValue("memberEmail"))
    

    Thanks,

    Jeavon

  • 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