Hello,
How can I get data from a table row (such as Id or other columns) on click method from a button that is in the same row? So I can pass an Id to the Delete method or something like that.
This is my .razor file:
And how can I read the data passed to the uri in the page I switched to? I have this code file, and the uri would be:
"/AddEditClient?id=5", and the expected (for me) doesn't happen.
Thank you.
Pablo
How can I get data from a table row (such as Id or other columns) on click method from a button that is in the same row? So I can pass an Id to the Delete method or something like that.
This is my .razor file:
ClientList.razor:
@page "/ClientList"
@using WebAPICountries.Models
@using WebAPICountries.Blazor.Services
@inject NavigationManager Navigator
<h2>Client List</h2>
<button type="button" class="btn-primary btn-sm" @onclick="AddNewClient">Add New Client</button>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
<th>Hire Date</th>
<th>City</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var client in clients)
{
<tr>
<td>@client.Name</td>
<td>@client.Department?.Name</td>
<td>@client.Salary</td>
<td>@client.HireDate</td>
<td>@client.City?.Name</td>
<td> <button type="button" class="btn-secondary btn-sm" @onclick="UpdateClient">Update</button></td>
<td> <button type="button" class="btn-danger btn-sm" @onclick="DeleteClient">Delete</button></td>
</tr>
}
</tbody>
</table>
@code {
private List<Client>? clients;
private ClientService? clientService;
protected override void OnInitialized()
{
clientService = new ClientService();
clients = clientService.GetAllClients();
}
private void AddNewClient()
{
Navigator.NavigateTo("/AddEditClient?id=0");
}
private void UpdateClient()
{
Navigator.NavigateTo("/AddEditClient?id=WHAT?");
}
private void DeleteClient()
{
clientService.DeleteClient(0);
}
}
"/AddEditClient?id=5", and the expected (for me) doesn't happen.
C#:
@page "/AddEditClient"
<h2>@title</h2>
@code {
private int id;
private string? title;
protected override void OnInitialized()
{
if (id == 5)
title = "Add New Client";
else
title = "Update Client";
}
}
Pablo