Copied to clipboard

Flag this post as spam?

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


  • Gavin Williams 48 posts 221 karma points
    Feb 19, 2020 @ 11:33
    Gavin Williams
    0

    Umbraco Forms PreValue source issue

    I'm using a prevalue source in an Umbraco Form to render a list of options from a db table. I've noticed in the html markup that the Value Column is placed in both the list value and the label like so:

    <input type="radio" name="3e56b737-c3db-4413-9099-844d002fc3c9" id="3e56b737-c3db-4413-9099-844d002fc3c9_4" value="Secondary">
    <label for="3e56b737-c3db-4413-9099-844d002fc3c9_4">Secondary</label>
    

    I'd expect the input value to be the Key Column and the label text to be the Value Column. So it appears that the Key Column is completely ignored when rendering the list. Has anyone else encountered this? Am I doing something wrong or is there some other way to retrieve the Key Column value of the selected list item?

    NOTE: tried to post this under Umbraco Forms, but the category field appears to have disappeared.

  • Gavin Williams 48 posts 221 karma points
    Feb 20, 2020 @ 17:35
    Gavin Williams
    100

    Ok I found out how to fix this. Realised the markup is controlled by the partial view in Views\Partials\Forms\Themes\default\Fieldtypes\FieldType.RadioButtonList.cshtml and DropDownList.cshtml.

    So the line that reads:

    <input type="radio" name="@Model.Id" id="@string.Concat(Model.Id,"_",i)" value="@pv.Value" 
    

    needs to read:

    <input type="radio" name="@Model.Id" id="@string.Concat(Model.Id,"_",i)" value="@pv.Id" 
    

    HOWEVER: this is great for field values from a db table, but won't work for fields where you've specified a hardcoded list of prevalues. Therefore to handle both scenarios the above line becomes:

    var valueId = pv.Id == "0" ? pv.Value : pv.Id;
    <input type="radio" name="@Model.Id" id="@string.Concat(Model.Id,"_",i)" value="@valueId" 
    

    So if the Id is 0, it uses the string value as before (hardcoded prevalue ids are always zero), otherwise it uses the Id from prevalue source Id field.

    The caveat here is that this solution assumes you aren't populating any prevalues with a legitimate Id of 0.

Please Sign in or register to post replies

Write your reply to:

Draft