Copied to clipboard

Flag this post as spam?

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


  • Brett Palmer 8 posts 98 karma points
    Feb 07, 2022 @ 12:57
    Brett Palmer
    0

    Set fallback for dropdown

    I've simplified my use case to Umbraco's default's for rendering pages and blocklists. I've created a block called Fruit that has one field: a dropdown with the following options:

    "" Orange Banana Pear

    My code for rendering the Fruit block - and the one single field the block contains is:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>;
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @{
        var content = (ContentModels.Fruit)Model.Content;
        var settings = (ContentModels.Fruit)Model.Settings;
    }
    
    <div style="border:2px dashed red; padding: 10px; margin: 10px 0;">
        <div>Fruit: @(content.Value<string>("Cmp_Block_Fruit", fallback: Fallback.ToDefaultValue, defaultValue: "Apple"))</div>
    </div>
    

    So the idea is that all Fruit blocks - if they are left without a value in that dropdown field will be "Apple". If I add one Fruit block to the page without a value indicated in the dropdown: I get an apple. If I copy that Fruit block or independently add another Fruit block without a fruit indicated both the first Fruit block and the second fruit block render "".

    Suddenly the fallback code to render "Apple" instead of "" does not function because there's a second consecutive block on the page that makes use of the fallback?

    Here's a link to a screen capture of me experiencing the issue. I find it's a little blurry if you play over Google Drive but crisp if you were to download it. Maybe either way: it sets the stage.

    https://drive.google.com/file/d/1j3zGF4TdCNhVxxfRAF2IelI1zfSoXrq7/view?usp=sharing

    I could certainly use a different way to render "Apple". I could make the field mandatory and add "Apple" as the first selectable option. I know there are some plugins out there to build upon Umbraco's dropdowns re: default values - and maybe this is why? But I'm trying to keep this project lean and the CMS experience of no selection = "Apple" is what I'd like to be able to go with.

Please Sign in or register to post replies

Write your reply to:

Draft