Files
CartriX/CartriX.Web/Pages/LocationsAdd.razor
T

44 lines
1.4 KiB
Plaintext

@page "/LocationsAdd"
@using System.Net.Http.Json
@using System.ComponentModel.DataAnnotations
@inject HttpClient Http
@inject NavigationManager Nav
<EditForm Model="model" OnValidSubmit="Create">
<DataAnnotationsValidator />
<div style="max-width:400px;">
<RadzenLabel Text="Название местоположения" />
<RadzenTextBox @bind-Value="model.Name" Style="width:100%;" />
<ValidationMessage For="@(() => model.Name)" />
<div style="margin-top:12px;">
<RadzenButton ButtonType="ButtonType.Submit" Text="Создать" />
<RadzenButton ButtonType="ButtonType.Button" Text="Отмена" />
</div>
</div>
</EditForm>
@code {
private CreateLocationModel model = new();
private async Task Create()
{
var resp = await Http.PostAsJsonAsync("api/locations", new { Name = model.Name });
if (resp.IsSuccessStatusCode) Nav.NavigateTo("/locations");
else
{
var err = await resp.Content.ReadAsStringAsync();
// простая обработка ошибки
await JS.InvokeVoidAsync("alert", $"Ошибка: {err}");
}
}
public class CreateLocationModel
{
[Required(ErrorMessage = "Имя обязательно")]
[StringLength(200)]
public string Name { get; set; } = string.Empty;
}
[Inject] IJSRuntime JS { get; set; } = default!;
}