Copied to clipboard

Flag this post as spam?

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


  • Hundebol 167 posts 314 karma points
    Sep 09, 2014 @ 13:59
    Hundebol
    0

    Display properties from members

    Hi guys,

    I'm new in this razor world, but i'm trying do display some properties from members.

    I have currently two properties:

    A true/false property named "hideInMembersList"
    And a Upload property named "logo"

    I managed to get the true/false property to work, but not the upload-field. Any one have to code to spare?
    I want there to be an if-sentence because not all members will have an image uploaded.

    I guess it's super easy, but clearly doing something wrong. This i my currently working code:

    It is a Partial View Macro File

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;
    @{
      var memberservice = ApplicationContext.Current.Services.MemberService;
    }

        @foreach (Member item in memberservice.GetMembersByGroup("Members here"))
        {

    if (item.GetValue("hideInMembersList").ToString() == "0") {
    <p><strong><a href="?id=@item.Id">@item.Name</a></strong></p>
    }

        }

     

    I've tried the following:

    if(item.HasProperty("logo"))
                {
                    var logo = @item.getProperty("logo").Value.ToString();
                    <img src="@logo" />
                } 

    Which is working on a Scripting File - but not here. It just gives the "error loading macro partial

    Anyone who can help me out?

     

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Sep 09, 2014 @ 14:11
    Jeavon Leopold
    0

    How about using the type converters, something like this:

    @{
        var memberservice = ApplicationContext.Current.Services.MemberService;
    }
    
    @foreach (var member in memberservice.GetMembersByGroup("Members here"))
    {
    
        if (!member.GetValue<bool>("hideInMembersList"))
        {
            <p><strong><a href="?id=@member.Id">@member.Name</a></strong></p>
        }
    
        if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
        {
            var logo = member.GetValue<string>("logo");
            <img src="@logo" />
        }
    
    }
    
  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Sep 09, 2014 @ 14:19
    Jeavon Leopold
    0

    An alternative approach (if you don't need write access) might be to use the TypedMember helper, unfortunately there isn't a way to get members by group so we will still need the members service for this.

    The positive to this is that the Members will be returned as IPublishedContent, the same as Content so you have HasValue etc... e.g.

    @{
        var memberservice = ApplicationContext.Current.Services.MemberService;
    }
    
    @foreach (var member in memberservice.GetMembersByGroup("Members here"))
    {
        var typedMember = Umbraco.TypedMember(member.Id);
    
        if (!typedMember.GetPropertyValue<bool>("hideInMembersList"))
        {
            <p><strong><a href="?id=@typedMember.Id">@typedMember.Name</a></strong></p>
        }
    
        if (typedMember.HasValue("logo"))
        {
            var logo = typedMember.GetPropertyValue<string>("logo");
            <img src="@logo" />
        }
    
    }
    
  • Hundebol 167 posts 314 karma points
    Sep 09, 2014 @ 15:00
    Hundebol
    0

    Uttermost respect for that answer. Quick and perfectly working - both examples! Big thanks...

    I guess, this makes my razor skills rather much in the dust - i have no idea how you came to those to snippets.
    And I'm not sure iI even get why both of them work - and whats the difference between the two?!

    Could you maybe shortly elaborate before I mark an answer? Which is the best practice?

    Again, a huge thank you!

  • Hundebol 167 posts 314 karma points
    Sep 09, 2014 @ 15:37
    Hundebol
    0

    Just another follow up - the first code shows all the members with the "hideInMembersList" not checked (correct)
    The second code shows ONLY the members with the "hideInMembersList" checked (not correct)

    Other than that - i have difficulty turning this into a list! You know <li></li>

    I have rearranged and changed the code a bit, but why on earth does is make the "error loading..." because I add some HTML

     

    Working

        @foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
        {
    
        if (!member.GetValue<bool>("hideInMembersList"))
        {
    
            <p><strong><a href="?id=@member.Id">@member.Name</a></strong></p>
    
            if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
            {
                var logo = member.GetValue<string>("logo");
                <img src="@logo" />
            }
    
        }
    
    }
    

    Not working

    <ul>
        @foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
        {
        <li>  
        if (!member.GetValue<bool>("hideInMembersList"))
        {
    
            <p><strong><a href="?id=@member.Id">@member.Name</a></strong></p>
    
            if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
            {
                var logo = member.GetValue<string>("logo");
                <img src="@logo" />
            }
    
        }
    </li>
    }
    </ul>

    Thank you in advance!

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Sep 09, 2014 @ 15:55
    Jeavon Leopold
    100

    You're very welcome. I have fixed my second snippet above (I forgot the ! for the NOT operator).

    So MemberService is a read/write API returning Member, it directly queries the database. TypedMember and most of the MemberShipHelper use IPublishedContent (read only) which is cached so doesn't use the database so is far quicker.

    This should solve your html issue (just required an additional @ on the if to switch to code block):

    <ul>
        @foreach (var member in memberservice.GetMembersByGroup("FB Suppliers Member"))
        {
            <li>
                @if (!member.GetValue<bool>("hideInMembersList")){
    
                    <p><strong><a href="?id=@member.Id">@member.Name</a></strong></p>
    
                    if (!string.IsNullOrEmpty(member.GetValue<string>("logo")))
                    {
                        var logo = member.GetValue<string>("logo");
                        <img src="@logo" />
                    }
    
                }
            </li>
        }
    </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft