Copied to clipboard

Flag this post as spam?

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


  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 09:11
    TheLogan
    0

    Load page with parameter

    Heya, I'm listing out a grid of members and would like to add links to each member so you can read their profile (sort of like your average social site). However I'm pretty new to Umbraco and despite numerous searches, I haven't been able to find a way to do this. In standard ASP I would just load the 'viewprofile' page with a parameter of the member's ID, but I don't know how to do that in Umbraco.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 30, 2018 @ 09:33
    Michaël Vanbrabandt
    100

    Hi TheLogan,

    what you could do for this kind of task is creating a ProfileMemberSurfacecontroller where you can add an ActionMethod for rendering the profile of the logged in member.

    More docs about surfacecontrollers: https://our.umbraco.org/documentation/reference/routing/surface-controllers

    Then in your controller you could have something like:

    public class ProfileMemberSurfaceController : SurfaceController
    {
        [Authorize]
        public ActionResult RenderProfile()
        {
            // Custom Model for representing the data of the member in the view
            ProfileMemberViewModel profileModel = new ProfileMemberViewModel();
    
            var membershipService = ApplicationContext.Services.MemberService;
    
            // Check if the member is logged in
            if (Members.IsLoggedIn())
            {
                //Let's fill it up
                var currentMember = membershipService.GetById(Members.GetCurrentMemberId());
    
                profileModel.Name           = currentMember.Name;
                profileModel.EmailAddress   = currentMember.Email;
                profileModel.MemberID       = currentMember.Id;
            }
            else
            {
                // if the member is not logged in redirect back to the root page
                return Redirect("/");
            }
    
            // Pass in the model to the view
            return PartialView("ViewProfile", profileModel);
        }
    }
    

    Where in the last piece of code you call a view called ViewProfile where you pass the member details and render some html.

    In this example I used the MemberService to get the member from the database, you can also use the xml cache using:

    var member = Members.GetCurrentMember();
    var profile = Members.GetCurrentMemberProfileModel();
    

    But then I have to check if have access to all properties of the member, even the custom ones.

    Hope this helps.

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 12:07
    TheLogan
    0

    It looks resonable enough, though I have a problem with it.

    I'm in visual studio 2017 and followed the nuget setup method so things may be slightly different from the vscode. One thing for example that I've noticed is that I need to chose which files and folders to include in the project. When I first tried setting this up I ran into a problem where the controller couldn't find the Umbraco namespace, which was really confusing, but after some time I found that I needed to include the 'App_Data' folder in the project. Now it can find the Umbraco namespace and the Umbraco.Web namespace, however it can't find the Umbraco.Web.Mvc namespace, and having googled for solutions I still am not sure what I'm doing wrong xD

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 30, 2018 @ 12:10
    Michaël Vanbrabandt
    0

    Hi TheLogan,

    so if I understand you, you installed Umbraco fresh using Nuget?

    Where did you placed the controller files? In the App_Code folder?

    Thanks

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 12:14
    TheLogan
    0

    First I created a folder called Controller in the root and placed it in there, then I tried placing the file in the root. I didn't think of putting it inside the App_Code folder

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 12:16
    TheLogan
    0

    There's no AppCode folder, do you mean the AppData folder (there's also an AppBrowsers folder and an AppPlugins folder)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 30, 2018 @ 12:18
    Michaël Vanbrabandt
    1

    By default this is not created, you have to add this manually. Do this, right click your project and go to add > add folder > asp.net folder > app_code

    Hope this helps!

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 12:42
    TheLogan
    0

    That did the trick. Thank you very much! :)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 30, 2018 @ 13:01
    Michaël Vanbrabandt
    1

    Hi TheLogan,

    no problem, glad it solved your issue!

    Have a nice day and long weekend!

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 13:07
    TheLogan
    1

    Thank you, and you too! :)

  • TheLogan 16 posts 120 karma points
    Mar 30, 2018 @ 20:25
    TheLogan
    0

    For the rest of my site I'm using a master template, how do I apply it to a surface page? I tried this:

    @{
        Layout = "../MasterTemplate.cshtml";
    }
    

    But that gives me the following error:

    Cannot bind source type Umbraco.Core.Models.Member to model type Umbraco.Web.Models.RenderModel

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 31, 2018 @ 06:54
    Michaël Vanbrabandt
    0

    Hi TheLogan,

    because in my code example the view rendered by the SurfaceController is a partial view, you cannot use it like a normal view and apply a masterpage to it.

    You have to insert this partial view inside of one of your views from your Umbraco content section using:

     @Html.Action("RenderProfile", "ProfileMemberSurface")
    

    Hope this helps.

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 31, 2018 @ 09:53
    TheLogan
    0

    Ooh, ok. Then I have the same problem as before though .. I think xD I don't only want to allow members to view their own profile but also other members profile Currently it generates this url: http://localhost:34977/umbraco/Surface/ViewProfile/Profile/1088

    Where the last number is the ID for the member. How do I handle that?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 31, 2018 @ 10:23
    Michaël Vanbrabandt
    0

    Hi TheLogan,

    just add a param to your method int memberId which will take the id from your url.

    Then using this id you can get the correct member details.

    Hope this helps!

    /Michaël

  • TheLogan 16 posts 120 karma points
    Mar 31, 2018 @ 11:28
    TheLogan
    0

    Yeah, that's what I'm doing now :)

    public ActionResult Profile(int id)

    But that's for the surface.

    What I'm confused about is this:

    With your suggested setup I'll have 1) the original umbraco page 2) the intermediat umbraco page which calls the partial view 3) the partial view that renders the profile

    no 1 knows the ID number no 3 needs the ID number no 2 needs to take the id number from no 1 and give it to no 3.

    How do I do the handover from no 1 to no 3? I hope this made it clear, if not, let me know :)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 31, 2018 @ 11:50
    Michaël Vanbrabandt
    0

    Hi TheLogan,

    From your view page where you call the ActionMethod in your SurfaceController you can pass the id of the member like:

    @Html.Action("RenderProfile", "ProfileMemberSurface", new { id = YOURIDINT })
    

    Then you have the Id of the Member in your SurfaceController to get the details of the Member.

    Then in your ActionMethod where you call the View you pass in a ViewModel which will contain a property MemberId which you then assign using the param Id.

    In this way you will have the Id in your partial view rendered by your surface controller.

    Hope this helps.

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft