The following code works for me:
@if (data == null)
{
<p><em>Loading...</em></p>
}
else
{
<button class="btn btn-primary" @onclick="Save">Save all changes</button>
<table class="table">
<thead>
<tr>
<th>Concern ID</th>
<th>CDC</th>
<th>Context</th>
<th>Reporting</th>
</tr>
</thead>
<tbody>
@foreach (var c in data)
{
<tr>
<td>@c.ConcernId</td>
<td><input type="checkbox" @bind="@c.PassingAllowed" /></td>
<td><input type="checkbox" @bind="@c.ContextPassingAllowed" /></td>
<td><input type="checkbox" @bind="@c.ReportingPassingAllowed" /></td>
</tr>
}
</tbody>
</table>
}
@code{
private ConcernData[] data;
protected override async Task OnInitializedAsync()
{
await GetData().ConfigureAwait(false);
}
private async Task GetData()
{
data = await Http.GetFromJsonAsync<ConcernData[]>("ConcernFilter").ConfigureAwait(false);
}
private async Task Save()
{
await Http.PostAsJsonAsync<ConcernData[]>("ConcernFilter/Update", data).ConfigureAwait(false);
}
private async Task Update(int concernId)
{
Console.Write(concernId);
}
}
However this sends over all the data (changed and unchanged) back to the server where I need to figure out (or simply update one by one) which data needs an update in the databases.
It doesn't feel right as I'm sending too much data over the wire and sending too many update statements to the databases (3 in this case).
I can think of several way to tackle this:
- on the client compare the altered list with an original list and only send the items to the server that really need an update.
- find a way to write in Blazor for a checkbox in such a list how to call the Update method and passing the correct concernId as a parameter.
I'm looking for help to accomplish option 2.