<pre>@page "/products/create" @inject HttpClient client @inject NavigationManager uriHelper <h1>Create new product</h1> <hr /> <EditForm Model="@model" OnValidSubmit="@validSubmit"> <DataAnnotationsValidator/> <div class="form-group"> <label>Code :</label> <div> <InputText @bind-Value="@model.Code"/> <ValidationMessage For="@(()=>model.Code)"/> </div> </div> <div class="form-group"> <label>Name :</label> <div> <InputText @bind-Value="@model.Name" /> <ValidationMessage For="@(()=>model.Name)" /> </div> </div> <div class="form-group"> <label>Category :</label> <div> <InputSelect @bind-Value="@model.Category"> @foreach (var item in categories) { <option>@item</option> } </InputSelect> <ValidationMessage For="@(()=>model.Category)" /> </div> </div> </EditForm> @code{ ProductsViewModel model = new ProductsViewModel(); string[] categories { get; set; } protected override async Task OnInitializedAsync() { categories = await client.GetFromJsonAsync<string[]>("api/products/categories"); } async Task validSubmit() { await client.PostAsJsonAsync("api/products", model); uriHelper.NavigateTo("products"); } }
ProductsController:
[Route("categories")] [HttpGet] public string[] GetCategories() { return categoriesService.GetAllAsync().Result.Select(e => e.Name).ToArray(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)