I'd like to render a table with inline editing of a single property in my Blazor (server-side) application. Each item in the collection should render row in a table wrapped with an EditForm tag so I get model binding of each collection along with an EventCallback for each "Edit" or "Delete" operation.
Here is my parent component which includes an IEnumerable<GroupModel>:
Parent Component:
@{
if (_allGroups.Any())
{
<div>
<table class="table" id="allGroups">
<thead>
<tr>
<th scope="col">Modify</th>
<th scope="col">Group Name</th>
<th scope="col">Associated (#)</th>
<th scope="col">Date Added</th>
</tr>
</thead>
<tbody>
@foreach (var item in _allGroups)
{
<EditGroupItem SaveItem="@SaveItemAsync" DeleteItem="@DeleteItemAsync" GroupItem="@item"/>
}
</tbody>
</table>
</div>
}
}
EditGroupItem component:
@using MyProject.Common.Models
<EditForm Model="GroupItem">
<tr>
<td>
<button type="button" class="btn btn-sm btn-info" @onclick="(() => SaveItem.InvokeAsync(GroupItem))"><i class="fas fa-save"></i></button>
<button type="button" class="btn btn-sm btn-danger" @onclick="(() => DeleteItem.InvokeAsync(GroupItem))"><i class="fa fa-trash"></i></button>
</td>
<td>
<InputText type="text" class="form-control" @bind-Value="@GroupItem.GroupName"></InputText>
</td>
<td>@GroupItem.PasswordsAssociatedTo</td>
<td>@GroupItem.DateAdded.ToString("d")</td>
</tr>
</EditForm>
@code {
[Parameter] public GroupModel GroupItem { get; set; }
[Parameter] public EventCallback<GroupModel> DeleteItem { get; set; }
[Parameter] public EventCallback<GroupModel> SaveItem { get; set; }
}
Given the code above, the table never shows each component instance created by the @foreach statement.
I tried removing the <EditForm> tags and just use a regular <input> instead of the Blazor <InputText> tag, and the component is rendered with the parameters just fine. However, my callback doesn't pass back the edited value from @GroupItem.GroupName.
So the above code doesn't render correctly and the other option I tried doesn't give back the edited object.
Any ideas on how I can achieve inline editing of an object passed into a Blazor component? Or maybe another way altogether?