Copied to clipboard

Flag this post as spam?

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


  • Mark Watson 118 posts 384 karma points
    Jun 20, 2018 @ 05:29
    Mark Watson
    0

    Drop down menu returning number not string

    I am using Umbraco cloud and have created a drop down menu to select the state (NSW, VIC etc) in the members section and I am using a partial view.

    When the state is selected it displays a value and not the text (NSW)

    @foreach (var member in ApplicationContext.Current.Services.MemberService.GetMembersByMemberType("activeMembers"))
        {
        var prac1st = member.GetValue<string>("practice1State");
    <p> @prac1st </p>}
    

    How do I display the value NSW, VIC etc

  • jf 8 posts 103 karma points c-trib
    Jun 20, 2018 @ 09:54
    jf
    1

    If the dropdown list is made from prevalues as shown in the screenshot below you'll need to do some extra lookups in the code.

    enter image description here

        var memberStateDataType = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Member State - Dropdown list");
        var memberStateDictionary = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType.Id).PreValuesAsDictionary;
    
        foreach (var member in ApplicationContext.Current.Services.MemberService.GetMembersByMemberType("activeMembers"))
        {
            var stateProperty = member.Properties.FirstOrDefault(p => p.PropertyType.Alias == "practice1State");
            var selectedPreValueId = int.Parse((string)stateProperty.Value);
            var selectedPreValue = memberStateDictionary.FirstOrDefault(preValue => preValue.Value.Id == selectedPreValueId);
            // next line is the state e.g. "NSW"
            var selectedState = selectedPreValue.Value.Value;
        }
    

    It's not very elegant but if you're doing it in Razor I think this is what you have to do. If anyone else can provide something better I'd love to know.

  • krzakivan 27 posts 93 karma points
    Jan 10, 2020 @ 09:55
    krzakivan
    0

    Hello Jf

    Im dealing with the issue to render SELECTED VALUE to html .

        @if (Model.HasValue("productcategory"))
    {
    var memberStateDataType = Umbraco.DataTypeService.GetDataTypeDefinitionByName("productcategory- Dropdown");
        var memberStateDictionary = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType.Id).PreValuesAsDictionary;
        foreach (var member in ApplicationContext.Current.Services.MemberService.GetMembersByMemberType("activeMembers"))
        {
            var stateProperty = member.Properties.FirstOrDefault(p => p.PropertyType.Alias == "practice1State");
            var selectedPreValueId = int.Parse((string)stateProperty.Value);
            var selectedPreValue = memberStateDictionary.FirstOrDefault(preValue => preValue.Value.Id == selectedPreValueId);
            // next line is the state e.g. "NSW"
            var selectedState = selectedPreValue.Value.Value;
        <p>@selectedState</p>
        }
    }
    

    Do you have a way how to render it simply ?

    Thank you.

  • Mark Watson 118 posts 384 karma points
    Jun 21, 2018 @ 00:20
    Mark Watson
    0

    Thanks jf

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies