Не вызывается событие при отправке формы
При нажатии кнопки "Удалить" не происходит удаление сотрудника по ID, введёному в input type=text. В чём ошибка?
@page "/employee"
@inject HttpClient Http
@inject IHttpClientFactory ClientFactory
<h3>Сотрудники</h3>
@if (errorMessage != null)
{
<div class="alert alert-danger">
@errorMessage
</div>
}
<div class="form_container">
<form @onsubmit="DeleteEmployee">
<label>ID сотрудника:</label>
<input type="text" @bind="id" required>
<button type="submit">Удалить</button>
</form>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>ФИО</th>
<th>Дата рождения</th>
</tr>
</thead>
<tbody>
@if (items == null)
{
<tr>
<td colspan="3">Loading...</td>
</tr>
}
else
{
foreach (var item in items)
{
<tr>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.Birthday.ToString("dd-MM-yyyy")</td>
</tr>
}
}
</tbody>
</table>
@code {
private List<Employee> items;
private string errorMessage;
private string id;
private async Task load()
{
var client = ClientFactory.CreateClient("MyAPI");
try
{
items = await client.GetFromJsonAsync<List<Employee>>("api/employee");
}
catch (Exception ex)
{
errorMessage = "Не удалось загрузить данные: " + ex.Message;
}
}
protected override async Task OnInitializedAsync()
{
await load();
}
private async Task DeleteEmployee()
{
var client = ClientFactory.CreateClient("MyAPI");
if (int.TryParse(id, out int delid))
{
try
{
var response = await client.DeleteAsync($"api/employee/{delid}");
if (response.IsSuccessStatusCode)
{
await load();
}
else
{
errorMessage = "Не удалось удалить сотрудника";
}
}
catch (Exception ex)
{
errorMessage = "Не удалось загрузить данные: " + ex.Message;
}
}
else
{
errorMessage = "Введены некоректные данные";
}
}
}