diff --git a/Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementSingleChoiceModel.cs b/Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementSingleChoiceModel.cs index ed9b73937..6f4ce5191 100644 --- a/Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementSingleChoiceModel.cs +++ b/Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementSingleChoiceModel.cs @@ -1,6 +1,7 @@ using CO.CDP.OrganisationApp.Models; using Microsoft.AspNetCore.Mvc; using System.ComponentModel.DataAnnotations; +using System.Text.Json; namespace CO.CDP.OrganisationApp.Pages.Forms; @@ -46,18 +47,33 @@ public override void SetAnswer(FormAnswer? answer) { case nameof(FormAnswer.OptionValue): value = answer?.OptionValue; + + if (value != null && Options?.Choices != null && Options.Choices.ContainsKey(value)) + { + SelectedOption = value; + } + break; case nameof(FormAnswer.JsonValue): value = answer?.JsonValue; + + if (value != null && Options?.Choices != null) + { + // We have to deserialize and serialize the json before checking equality, + // because the representation straight out of postgres does not match the representation from C# with respect to whitespace + Dictionary? parsedValue = JsonSerializer.Deserialize>(value); + string reseralizedValue = JsonSerializer.Serialize(parsedValue); + + if (Options.Choices.ContainsKey(reseralizedValue)) + { + SelectedOption = reseralizedValue; + } + } + break; default: throw new InvalidOperationException($"Unsupported field: {Options?.ChoiceAnswerFieldName}"); } - - if (value != null && Options?.Choices != null && Options.Choices.ContainsKey(value)) - { - SelectedOption = value; - } } public IEnumerable Validate(ValidationContext validationContext) diff --git a/Frontend/CO.CDP.OrganisationApp/Pages/Forms/_FormElementCheckBoxInput.cshtml b/Frontend/CO.CDP.OrganisationApp/Pages/Forms/_FormElementCheckBoxInput.cshtml index 929b2e0c8..ce102e82f 100644 --- a/Frontend/CO.CDP.OrganisationApp/Pages/Forms/_FormElementCheckBoxInput.cshtml +++ b/Frontend/CO.CDP.OrganisationApp/Pages/Forms/_FormElementCheckBoxInput.cshtml @@ -31,7 +31,7 @@