samedi 17 septembre 2022

Checkboxes. Blazor. ArgumentException: The provided expression contains a SimpleBinaryExpression which is not supported

Continuation from previous question. The solution provided by Lex for previous question worked fine and I've used that format for this task. However, I wanted to take it slightly further - also obtain the results from the user clicking checkboxes but this time each checkbox result would be stored in an array declared within the class. So I guess one layer deeper as it were. I don't know why, but when I nest a loop to cycle through each checkbox, I get the below error. If I write each of them out it works but of course I want it to be for any size bool array so only a loop would work.

ArgumentException: The provided expression contains a SimpleBinaryExpression which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object.

So my questions are, why do I get this error and is there an obvious workaround?

@page "/counter"

<div>
<EditForm Model="this.e">
    @for (int i = 0; i < this.e.Questions.Count; i++)
    {
            {
                <div>
                    @for (int j = 0; j < 1; j++) @*so this should produce four 
 checkboxes*@
                    {
                        <label>
                        <InputCheckbox @bind-Value="this.e.Questions[i].Answers[j] " />
                           A question would go above and here would be 1+ answer.
                        </label>
                    }
                </div>
            }
        }
</EditForm>
</div>
<div>

 Gives access to results like:
@e.Questions[1].Answers[0] @*first checkBox ticked, true/false*@
@e.Questions[1].Answers[1]
@e.Questions[1].Answers[0]
@e.Questions[1].Answers[1]

</div>

@code
{
    public class EditFormModel
    {
    public List<Question> Questions { get; set; }
}

public class Question
{
    public string? checkBoxtext { get; set; }

    public bool[] Answers { get; set; }
}


public EditFormModel e = new()
{
        Questions = new List<Question>
    {
        new()
        {
            checkBoxtext = "What is the capital of France?", // Two answers could be London, Paris
            Answers = new bool[] {
                true, true
            }
        },
        new()
        {
            checkBoxtext = "What is the capital of Scotland?", // Answers could be Edinburgh, Paris, Sydney
            Answers = new bool[] {
                true, true, true
            }
        }
    }
};

}




Aucun commentaire:

Enregistrer un commentaire