Copied to clipboard

Flag this post as spam?

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


  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 08:29
    Diego
    0

    Can't update member's data

    Hello everyone, i have a problem when i want to update member data.

    On the platform, the member can register and access the personal area. Here you can edit the data and save it. The problem arises when from the form the data should go to the controller with the "ProfileModel", but this arrives without some information: id, UserName, guiid etc.. Why? Anyone have a suggestion? Many thanks in advance.

    In .cshtml file

          using (Html.BeginUmbracoForm<UmbProfileController>("HandleUpdateProfile", new { RedirectUrl = profileModel.RedirectUrl }))
        {
    ....... form here
        }
    

    Here in the controller, "ProfileModel model" have much data empty or null.

     public async Task<IActionResult> HandleUpdateProfile([Bind(Prefix = "profileModel")] ProfileModel model)
        {
    .........
        }
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 08:51
    Huw Reddick
    0

    we would need to see your form to ascertain what you are passing back, you should add id, UserName, guiid etc as hidden fields on your form

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 09:01
    Diego
    0

    Into using there is a table with:

    <td>
    @if (!string.IsNullOrWhiteSpace(profileModel.UserName))
    {
    <div class="mb-3">
    <label asp-for="@profileModel.UserName" class="form-label"></label><br>
    <input asp-for="@profileModel.UserName" class="form-control" aria-required="true" disabled/>
    <span asp-validation-for="@profileModel.UserName" class="form-text text-danger"></span>
    </div>
    }
    </td>
    

    The input it's disable only for the front-end edit.

    For the exemple this value, will arrive like empty.

    So, you mean add all values and some all this like: guiid, username must be hidden?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 09:53
    Huw Reddick
    0

    How are you initializing your model?

    Are you doing something like

                    // Build a profile model to edit
                    ProfileModel = await _memberModelBuilderFactory
                        .CreateProfileModel()
                        // If null or not set, this will redirect to the current page
                        .WithRedirectUrl(null)
                        // Include editable custom properties on the form
                        .WithCustomProperties(true)
                        .BuildForCurrentMemberAsync()
    
  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 09:58
    Diego
    0

    Yes, in this way:

     // Build a profile model to edit
        var profileModel = await memberModelBuilderFactory
            .CreateProfileModel()
            // If null or not set, this will redirect to the current page
            .WithRedirectUrl(null)
            // Include editable custom properties on the form
            .WithCustomProperties(true)
            .BuildForCurrentMemberAsync();
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 10:17
    Huw Reddick
    0

    what usings/model import do you have in your view? could be you should be doing

    @Model.UserName
    
  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 12:05
    Diego
    0
    @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage    
    @using Umbraco.Cms.Core.Services
    @using Umbraco.Cms.Web.Common.Security
    @using Umbraco.Cms.Web.Website.Controllers
    @using Umbraco.Cms.Web.Website.Models
    @using Umbraco.Extensions
    @inject MemberModelBuilderFactory memberModelBuilderFactory;
    @inject IMemberExternalLoginProviders memberExternalLoginProviders
    @inject IExternalLoginWithKeyService externalLoginWithKeyService
    @{
    
        // Build a profile model to edit
        var profileModel = await memberModelBuilderFactory
            .CreateProfileModel()
            // If null or not set, this will redirect to the current page
            .WithRedirectUrl(null)
            // Include editable custom properties on the form
            .WithCustomProperties(true)
            .BuildForCurrentMemberAsync();
    
        var success = TempData["FormSuccess"] != null;
    }
    

    I use "profileModel" to bind properties with input tag. If i used "@Model.Username" for the exemple, the Model is a current page.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 12:14
    Huw Reddick
    0

    That's fine, just wanted to make sure.

    Are you using the built in controller and methods or have you created your own?

    UmbProfileController provides it's own HandleUpdateProfile so you shouldn't need to create one

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 12:40
    Diego
    0

    Being new to Umbraco, I still need to understand its logic well... so I created a custom controller to check what happens... In my controller I removed the prefix : Before

    public async Task<IActionResult> HandleUpdateProfile([Bind(Prefix = "profileModel")] ProfileModel model)
    

    After

    public async Task<IActionResult> HandleUpdateProfile(ProfileModel model)
    

    And now the "profileModel" that comes as a parameter is correctly set but without the changes!

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 13:00
    Huw Reddick
    0

    If you use the OOB macro code without creating your own controller, does it save changes?

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 13:08
    Diego
    0

    No, the profileModel doesn't arrive evaluated to the controller

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 13:23
    Huw Reddick
    0

    What error(s) are you receiving? What exact version of Umbraco do you have installed?

    I am currently using this OOB macro code in U10 and 11 without any issues

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 13:28
    Diego
    0

    I have no errors, I created a macro with "Edit profile" snippet and the prfofileModel is not evaluated in the controller. I'm using Umbraco v11.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 13:53
    Huw Reddick
    0

    Odd, I would expect an error back from your post at least if it is failing.

    I did notice this code you posted

    @if (!string.IsNullOrWhiteSpace(profileModel.UserName))
    {
    <div class="mb-3">
    <label asp-for="@profileModel.UserName" class="form-label"></label><br>
    <input asp-for="@profileModel.UserName" class="form-control" aria-required="true" disabled/>
    <span asp-validation-for="@profileModel.UserName" class="form-text text-danger"></span>
    </div>
    }
    

    I have discovered that this will cause an error because when disabled it fails to send the username in the post. Try setting readonly instead of disabled, does your post then work?

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 15:26
    Diego
    0

    I've removed disabled and Username is ok. But all member's properties, key, creation date and others... have not a value.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 31, 2023 @ 16:15
    Huw Reddick
    0

    Those values will only get passed to the controller if they exist in your form post, so if you want to access them there then you need to add them as hidden fields in your form.

  • Diego 25 posts 156 karma points
    Jul 31, 2023 @ 17:59
    Diego
    0

    There is a loop on the member properties which are also bound to their input tags. So the values are changeable from the front-end but not pass to the controller

    for (var i = 0; i < profileModel.MemberProperties.Count; i++)
    {
     @Html.LabelFor(x => profileModel.MemberProperties[i].Value, profileModel.MemberProperties[i].Name)
     <input asp-for="@profileModel.MemberProperties[i].Value" class="form-control" />
    }
    

    At the end of everything, out of the table tag but into "using":

    <button type="submit" class="btn btn-primary ">Update</button>
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 01, 2023 @ 07:53
    Huw Reddick
    0

    I haven't personally looked into the Umbraco source to see what the UmbProfileController does, but if I use that macro and update anything it all gets updated correctly when the form is posted, even custom properties.

    Have you downloaded the Umraco source code in order to determine this?

  • Diego 25 posts 156 karma points
    Aug 01, 2023 @ 09:32
    Diego
    100

    In the .cshtml file where the member can edit their profile, a condition for excluding some properties, created the problem. Having moved this to the block at the top of the file:

    @{
    var list = profileModel.MemberProperties.Where(x => x.Alias.Equals("prop1") ||
                                                        x.Alias.Equals(("prop2"))
                                                        ).ToList();
    } 
    

    the problem can be said to be solved.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft