Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 22, 2021 @ 10:29
    Simon Dingley
    0

    Custom DropDown Property Editor Model Value is Dropdown Text instead of Value

    I have a project with a custom property editor. I've not really done much in the way of Angular for quite some time especially in this version of Umbraco but for some reason the model Umbraco is passing to the view is not correct, it contains the text value and not the id for the item as is stored in the property data in the database.

    The view looks like this:

    <div ng-controller="WiP.PropertyEditors.BranchDropDownController">
    
        <select name="dropDownList"
                class="umb-editor umb-dropdown"
                ng-model="model.value"
                ng-disabled="model.disabled"
                ng-options="branch.name for branch in model.branches track by branch.id">
        </select>
    
    </div>
    

    The controller can be reduced down to this and the issue is still present:

    function branchDropDownController($scope, $routeParams, $filter, entityResource, contentResource, branchResource, memberDataAccessResource) {
    
        var vm = this;
    
        branchResource.getBranches().then(function (result) {
            $scope.model.branches = result.data;
        });
    
    }
    
    angular.module('umbraco').controller('MyProject.PropertyEditors.BranchDropDownController', branchDropDownController);
    

    I've checked the database and the property data is correct and stores the id which is an integer but the model passed to the property editor is returning the text instead of the id and I can't work out how/why or where its even getting the text value from?

    Am I missing something very obvious here?

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 22, 2021 @ 13:10
    Simon Dingley
    0

    I'll leave this here for my future self and anyone else that might be interested or find it useful!

    The problem was actually a bug of sorts that was occurring as a result of customisation in this site to make member data read-only for all but a subset of Users in a particular user group. This was achieved by hooking into the EditorModelEventManager.SendingMemberModel event and checking the current users group membership and if not in a particular group would make member properties render using the readonlyvalue view. In this event handler was also the following piece of code:

    // If  we are showing a readonly value the id of the branch is no good
    // so we will get the branch name and return that instead.
    if (p.Alias.Equals("Branch"))
    {
        // Convert the BranchId to the Branch name
        if (int.TryParse(p.Value.ToString(), out var branchId))
        {
            p.Value = BranchHelper.GetBranch(branchId).Name;
        }
    }
    

    ...the problem is that it fell outside the permissions check and was being applied for all users and all of the time. Moving it inside the permissions check resolved the issue and stopped tampering with the model data therefore returning the expected value data again.

Please Sign in or register to post replies

Write your reply to:

Draft