Copied to clipboard

Flag this post as spam?

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


  • Josh Olson 79 posts 207 karma points
    May 09, 2014 @ 14:37
    Josh Olson
    0

    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!

    Cheers! - Josh

  • Bárður Joensen 7 posts 28 karma points
    May 20, 2014 @ 00:19
    Bárður Joensen
    1

    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)
    }

  • Josh Olson 79 posts 207 karma points
    May 20, 2014 @ 08:19
    Josh Olson
    0

    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

  • Ilya 18 posts 53 karma points
    Aug 19, 2014 @ 19:02
    Ilya
    0

    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! 

  • Josh Olson 79 posts 207 karma points
    Aug 20, 2014 @ 10:08
    Josh Olson
    0

    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!

  • Ilya 18 posts 53 karma points
    Aug 20, 2014 @ 12:27
    Ilya
    0

    Thanks, Josh.

    Well, maybe somebody can share some good ProfileModel experience? We both need your help, guys!

  • Anja Beisel 8 posts 28 karma points
    Oct 19, 2015 @ 17:32
    Anja Beisel
    0

    Doesn't seem a lot documentation out about Members.GetCurrentMemberProfileModel();

    So i went the basic way:

    @{
       var profileModel = Members.GetCurrentMemberProfileModel();
    }
    @foreach (var mp in profileModel.MemberProperties)
    {
       <div>@mp.Name @mp.Value</div>
    }
    

    @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:

    @foreach (var mp in profileModel.MemberProperties)
    {
       <div class="form-group">
          <div class="col-sm-3">
             @if (mp.Name == "gamepoints")
             {
                 @:Enter your target score
             }
             else
             {
                 @mp.Name
             }
           </div>
           <div class="col-sm-9">
              @if (mp.Name == "gamepoints")
              {
                 @*Data Type of the 'gamepoints' Property is 'Numeric'*@
                 @Html.TextBoxFor(m => mp.Value, new { Type = "number", Class = "form-control", Placeholder = "Enter your targer score" })
              }
              else
              {
                 @Html.TextBoxFor(m => mp.Value)
              }
           </div>
        </div>
     }
    

    If this is of any help for you, it would be a great pleasure for me. :)

  • Marko Jovanov 10 posts 114 karma points
    Nov 26, 2015 @ 13:49
    Marko Jovanov
    1

    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 extending ProfileModel would be more appropriate approach.

    Here is my take at updating the profile with MemberService:

    /// <summary>
    /// Verifies and edits the member fields.
    /// </summary>
    /// <param name="model"></param>
    /// <returns>MemberDetailsFormViewModel containing all the editable information.</returns>
    [Authorize]
    public ActionResult HandleUpdateMemberDetails(MemberDetailsFormViewModel model)
        {
            if (ModelState.IsValid == false)
            {
                return CurrentUmbracoPage();
            }
    
            var memberService = Services.MemberService;
            var member = memberService.GetById(Members.GetCurrentMemberId());
    
            member.Properties[Constant.PropertyAlias.Authentication.FirstName].Value    = model.FirstName;
            member.Properties[Constant.PropertyAlias.Authentication.LastName].Value     = model.LastName;
            member.Properties[Constant.PropertyAlias.Authentication.AddressLine1].Value = model.AddressLine1;
            member.Properties[Constant.PropertyAlias.Authentication.AddressLine2].Value = model.AddressLine2;
            member.Properties[Constant.PropertyAlias.Authentication.TownCity].Value     = model.TownCity;
            member.Properties[Constant.PropertyAlias.Authentication.PostCode].Value     = model.PostCode;
            member.Properties[Constant.PropertyAlias.Authentication.County].Value       = model.County;
            member.Properties[Constant.PropertyAlias.Authentication.Country].Value      = model.Country;
            member.Properties[Constant.PropertyAlias.Authentication.PhoneNumber].Value  = model.PhoneNumber;
    
            memberService.Save(member);
    
            if (Members.IsLoggedIn())
            {
                ViewBag.DetailSuccessfullyChanged = 1;
                return CurrentUmbracoPage();
            }
    
            return View("/");
    }
    

    StackOverflow post reference.

  • Harish 15 posts 74 karma points
    Mar 01, 2016 @ 12:09
    Harish
    0

    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

  • Harish 15 posts 74 karma points
    Mar 01, 2016 @ 11:57
    Harish
    0

    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....

  • Marko Jovanov 10 posts 114 karma points
    Mar 07, 2016 @ 14:20
    Marko Jovanov
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft