All documentation regarding using two way binding claims that in order to have two way binding in a custom component you need to use an EventCallback. And yet when using a List it works automatically. I would like to know why?
Example Page index.razor:
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt ListOfValues="@ListOfValues" StringValue="@StringValue" />
<div @onclick="WriteValues">then click me</div>
@code {
List<string> ListOfValues { get; set; } = new();
public string StringValue { get; set; } = "";
private void WriteValues()
{
foreach(var v in ListOfValues){
Console.WriteLine($"ListOfValues: {v}");
}
Console.WriteLine($"StringValue: {StringValue}");
}
}
Example component SurveyPrompt.razor:
<div class="alert alert-secondary mt-4">
<div @onclick="bubu">click me</div>
</div>
@code {
[Parameter] public List<string> ListOfValues { get; set; } = new();
[Parameter] public string StringValue { get; set; } = "";
private void bubu()
{
ListOfValues.Add("First Value set in a component");
ListOfValues.Add("Second Value set in a component");
StringValue = "A Value set in a component";
}
}
When running the app and clicking on "click me" and then clicking on "then click me" The console will show that the ListOfValues variable has values in index.razor that were filled in SurveyPrompt.razor but StringValue remains empty
How page looks:
How console looks after clicking on "click me" and then on "then click me":

