U7 adding custom member properties to the ProfileModel
New to MVC and so expect some oversights and misunderstandings here, but I am trying to build a site that makes heavy use of custom member properties. Using the new Members goodness and some of the Partial View snippets for doing member registration, login, etc., I have managed to get the basics working.
What I am trying to do now is to add custom properties into the scenario and allow members to make updates to those properties. Here is an example of the simplest situation: extra data about a member (first/last name).
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Web.Mvc.Html
@using ClientDependency.Core.Mvc
@using Umbraco.Web
@using Umbraco.Web.Controllers
@{
var profileModel = Members.GetCurrentMemberProfileModel();
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
//Html.RequiresJs("/umbraco_client/ui/jquery.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
var success = TempData["ProfileUpdateSuccess"] != null;
}
@if (Members.IsLoggedIn() && profileModel != null)
{
using (Html.BeginUmbracoForm<UmbProfileController>("HandleUpdateProfile", null, new { id = "updateProfileForm" }))
{
var member = Members.GetByUsername(profileModel.UserName);
var firstName = member.GetPropertyValue("firstName");
var lastName = member.GetPropertyValue("lastName");
... <extra markup removed for brevity> ...
@Html.TextBoxFor(m => profileModel.Name, new { @class = "form-control", placeholder = "Display Name", data_val_required="The Display Name field is required.", data_val="true" })
@Html.TextBoxFor(m => profileModel.Email, new { @class = "form-control", placeholder = "Email", data_val_required="The Email field is required.", data_val="true" })
@Html.TextBoxFor(m => firstName, new { @class = "form-control", placeholder = "First Name", data_val_required = "The First Name field is required.", data_val = "true" })
@Html.TextBoxFor(m => lastName, new { @class = "form-control", placeholder = "Last Name", data_val_required = "The Last Name field is required.", data_val = "true" })
<button>Save</button>
}
}
Obviously since my two custom properties (firstName and lastName) don't exist in the ProfileModel, when I hit the save button the two ProfileModel properties are updated and the two custom properties are not. From what I understand of MVC at this point, what I need to do is create my own model which inherits Umbraco.Web.Controllers.ProfileModel and adds the custom properties I want to include. So I try this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Cutom Model of Members
/// </summary>
public class MyProfileModel : Umbraco.Web.Models.ProfileModel
{
[Required]
[Display(Name = "First Name")]
public string firstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string lastName { get; set; }
}
and try to get things started with MyProfileModel profileModel = Members.GetCurrentMemeberProfileModel(); thinking that I could then access the custom properties as with the built-in ones, but alas and alack, no love. GetCurrentMembersProfileModel() returns a ProfileModel, not a MyProfileModel (obviously) and of course goes boom (cannot implicitly convert type blah blah...)!
I am now kinda stuck. Any pointers would be much appreciated!
I added the custom properties to my Member type in backoffice. Then, on the "Info" tab on the Member type, I marked "Member can edit" for those properties.
In my form, I'm iterating the properties like this:
Thanks Bárður. I had managed that much also. I can easily list out the properties (custom or otherwise) either using the method you describe or by accessing each property itself.
I think that I have found the path to the solution in using the MemberService service rather than mucking about in trying to mess with the ProfileModel, but I won't be able to test things out for a couple more days as I am currently busy on other tasks at the moment.
I am sure that there is a way to extend the ProfileModel to do what I was trying to do, but in the end I did not follow that path. I ended up using the MemberService to get the job done. If someone else has some hints/ideas on the right way to leverage the ProfileModel, I would still be interested in seeing how it is properly done though...
U7 adding custom member properties to the ProfileModel
New to MVC and so expect some oversights and misunderstandings here, but I am trying to build a site that makes heavy use of custom member properties. Using the new Members goodness and some of the Partial View snippets for doing member registration, login, etc., I have managed to get the basics working.
What I am trying to do now is to add custom properties into the scenario and allow members to make updates to those properties. Here is an example of the simplest situation: extra data about a member (first/last name).
Obviously since my two custom properties (firstName and lastName) don't exist in the ProfileModel, when I hit the save button the two ProfileModel properties are updated and the two custom properties are not. From what I understand of MVC at this point, what I need to do is create my own model which inherits
Umbraco.Web.Controllers.ProfileModel
and adds the custom properties I want to include. So I try this:and try to get things started with
MyProfileModel profileModel = Members.GetCurrentMemeberProfileModel();
thinking that I could then access the custom properties as with the built-in ones, but alas and alack, no love.GetCurrentMembersProfileModel()
returns a ProfileModel, not a MyProfileModel (obviously) and of course goes boom (cannot implicitly convert type blah blah...)!I am now kinda stuck. Any pointers would be much appreciated!
Cheers! - Josh
Hi Josh
I added the custom properties to my Member type in backoffice. Then, on the "Info" tab on the Member type, I marked "Member can edit" for those properties.
In my form, I'm iterating the properties like this:
@for (var i = 0; i < profileModel.MemberProperties.Count; i++)
{@Html.LabelFor(m => profileModel.MemberProperties[i].Value, profileModel.MemberProperties[i].Name)
@Html.EditorFor(m => profileModel.MemberProperties[i].Value)
@Html.HiddenFor(m => profileModel.MemberProperties[i].Alias)
}
Thanks Bárður. I had managed that much also. I can easily list out the properties (custom or otherwise) either using the method you describe or by accessing each property itself.
I think that I have found the path to the solution in using the MemberService service rather than mucking about in trying to mess with the ProfileModel, but I won't be able to test things out for a couple more days as I am currently busy on other tasks at the moment.
Cheers
Hi, Josh.
Have you found a solution? is it a right way to manage custom properties via profileModel (some code similar to what Bárður suggested)?
Thanks!
Hi Ilya,
I am sure that there is a way to extend the ProfileModel to do what I was trying to do, but in the end I did not follow that path. I ended up using the MemberService to get the job done. If someone else has some hints/ideas on the right way to leverage the ProfileModel, I would still be interested in seeing how it is properly done though...
Cheers!
Thanks, Josh.
Well, maybe somebody can share some good ProfileModel experience? We both need your help, guys!
Doesn't seem a lot documentation out about Members.GetCurrentMemberProfileModel();
So i went the basic way:
@mp.Alias doesn't return a value.
@mp.Name returns the Property Alias of the Member Type
I choose the editor manually depending on the Property Alias as follows:
If this is of any help for you, it would be a great pleasure for me. :)
I am experiencing the same issue.
Has anyone managed to extend the
ProfileModel
in order to add new properties?I have also managed to update the user through
MemberService
, but it seems to me that extendingProfileModel
would be more appropriate approach.Here is my take at updating the profile with
MemberService
:StackOverflow post reference.
Hi Marko,
Can you please paste the information for MemberDetailsFormViewModel class file and and a View which is relating to that model....if you dont mind
Hi Marko Jovanov,
I want to edit member details, even the member is not logged in as well.
Here is my below code.
var profileModel = ApplicationContext.Current.Services.MemberService.GetById(memberId);
@if (profileModel != null) {
using (Html.BeginUmbracoForm
An error is thrown stating that The property Umbraco.Core.Models.IMember.Email could not be found.
Any help should be appreciated....
Hi Harish,
I will have to take a look at the implementation since it was quite some time I've worked on this. I will let you know during this week.
Cheers, Marko
is working on a reply...