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.
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.
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.
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
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
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
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:
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?
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 :)
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.
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.
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:
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:But then I have to check if have access to all properties of the member, even the custom ones.
Hope this helps.
/Michaël
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
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
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
There's no AppCode folder, do you mean the AppData folder (there's also an AppBrowsers folder and an AppPlugins folder)
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
That did the trick. Thank you very much! :)
Hi TheLogan,
no problem, glad it solved your issue!
Have a nice day and long weekend!
/Michaël
Thank you, and you too! :)
For the rest of my site I'm using a master template, how do I apply it to a surface page? I tried this:
But that gives me the following error:
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:
Hope this helps.
/Michaël
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?
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
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 :)
Hi TheLogan,
From your view page where you call the
ActionMethod
in your SurfaceController you can pass the id of the member like: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
is working on a reply...