The main objectives of this article are:
- Implement HTTP Post Endpoint.
- In Blazor WebAssembly Create A Form
Implement Insert Logic In Service Files In API Project:
Let's add a method definition like 'CreateSuperHeroesAsync()' in 'ISuperHeroesService.cs'
API_Project/Services/ISuperHeroesService.cs:
Task<SuperHeroes> CreateSuperHeroesAsync(SuperHeroes newSuperHeroes);Let's implement the 'CreateSuperHeroesAsync()' in 'SuperHeroesService.cs'
API_Project/Services/SuperHeroesService.cs:
public async Task<SuperHeroes> CreateSuperHeroesAsync(SuperHeroes newSuperHeroes) { _myWorldDbContext.SuperHeroes.Add(newSuperHeroes); await _myWorldDbContext.SaveChangesAsync(); return newSuperHeroes; }
- Here we save the new item into the database.
Create HTTP Post Endpoint In API Project:
Let's add a new action method i.e HTTP Post endpoint in our 'SuperHeroesController.cs'
API_Project/Controllers/SuperHeroesController.cs:
[HttpPost] public async Task<IActionResult> PostAsync(SuperHeroes newSuperHeroes) { var result = await _superHeroesService.CreateSuperHeroesAsync(newSuperHeroes); return Ok(result); }
Add A Form To Create New Item In the Blazor WebAssembly Application:
Add a new blazor component like 'AddSuperHeroes.razor' in 'Pages/SuperHeroes'(folder).
Let's invoke the HTTP Post endpoint in 'AddSuperHeroes.razor'.
BlazorWasm_Project/Pages/SuperHeroes/AddSuperHeroes.razor:(HTML Part)
@page "/superheroes/add" @inject HttpClient Http @inject NavigationManager navigation <div class="ma-6 d-flex justify-center"> <MudChip Color="Color.Primary"> <h3>Create A New Super Hero</h3> </MudChip> </div> <div class="ma-6 d-flex justify-center"> <EditForm Model="@superHeroesFormModel" OnSubmit="CreateAsync"> <MudCard Class="d-flex justify-center" Width="800px"> <MudCardContent Class="d-flex flex-column"> <MudTextField Label="Name" Class="mt-2" @bind-Value="superHeroesFormModel.Name" For="@(() => superHeroesFormModel.Name)" /> <MudTextField Label="Image URL" Class="mt-2" @bind-Value="superHeroesFormModel.ImageURL" For="@(() => superHeroesFormModel.ImageURL)" /> <MudSelect Margin="Margin.Dense" Class="mt-2" T="string" Label="Franchise" Variant="Variant.Text" @bind-Value="superHeroesFormModel.Franchise"> <MudSelectItem Value="@("dc")">DC</MudSelectItem> <MudSelectItem Value="@("marvel")">Marvel</MudSelectItem> </MudSelect> <MudField Label="Gender" Class="mt-2" Variant="Variant.Text" InnerPadding="false"> <MudRadioGroup T="string" @bind-SelectedOption="superHeroesFormModel.Gender"> <MudRadio T="string" Option="@("male")" Color="Color.Primary">Male</MudRadio> <MudRadio T="string" Option="@("female")" Color="Color.Primary">Female</MudRadio> <MudRadio T="string" Option="@("others")" Color="Color.Primary">Others</MudRadio> </MudRadioGroup> </MudField> <MudButton Variant="Variant.Filled" ButtonType="ButtonType.Submit" Color="Color.Primary" Class="mt-2">Primary</MudButton> </MudCardContent> <MudCardContent Class="d-flex"> <div class="d-flex justify-center"> <MudImage ObjectFit="ObjectFit.Cover" Height="200" Width="250" Src="@superHeroesFormModel.ImageURL" Elevation="25" Class="rounded-lg" /> </div> </MudCardContent> </MudCard> </EditForm> </div>
- (Line: 1) Using '@page' directive defined route like '/superheroes/add'
- (Line: 2) Injected the 'HttpClient' instance
- (Line: 3) Injected the 'NavigationManager' instace.
- (Line: 5-40) Rendered our create form using mudblazor component.
- Here '@bind-value' value enables 2way binding with text field and dropdown component
- Here '@bind-SelectedOption' enables 2way binding for radio button group component.
- (Line: 12) The 'EditForm' is the blazor form component, we have to assign a model for the form by decorating the 'Model' attribute. Here form submission registered with 'OnSubmit' event.
@code { SuperHeroesVM superHeroesFormModel = new SuperHeroesVM(); private async Task CreateAsync() { await Http.PostAsJsonAsync<SuperHeroesVM>("/api/superheroes", superHeroesFormModel); navigation.NavigateTo("/superheroes/home"); } }
- Here 'SuperHeroesVm' model is our form model, which we registered with 'Model' attribute of 'EditForm'.
- Here we invoke the HTTP POST endpoint where we pass our form model as the payload.
- After saving the item we will navigate to 'AllSuperHeroes.razor' component.
Configure 'Add' Button In AllSuperHeroes Component:
Now let's add the 'Add' button in AllSuperHeroes component.
BlazorWasm_Project/Pages/SuperHeroes/AllSuperHeroes.razor:
@page "/superheroes/home" @inject HttpClient Http @inject NavigationManager navigation; <MudContainer Class="ma-4"> <div class="d-flex ma-2 justify-center"> <MudFab Color="Color.Primary" Label="Add" @onclick="@(() => {navigation.NavigateTo("/superheroes/add");})" /> </div> <div class="d-flex flex-wrap justify-center"> @foreach (var item in allHeroes) { <MudCard Width="250px;" Class="ma-2"> <MudCardHeader> <CardHeaderAvatar> <MudAvatar Color="Color.Secondary">@item.Id</MudAvatar> </CardHeaderAvatar> <CardHeaderContent> <MudText Typo="Typo.body1">@item.Name</MudText> </CardHeaderContent> </MudCardHeader> <MudCardMedia Image="@(item.ImageURL)" Height="250" /> <MudCardContent> <MudText Typo="Typo.body2">Franchise - @item.Franchise</MudText> <MudText Typo="Typo.body2">Gender - @item.Gender</MudText> </MudCardContent> <MudCardActions> </MudCardActions> </MudCard> } </div> </MudContainer>
- (Line:3) Injected the 'NavigationManager' instance
- (Line: 6-8) Configure the 'Add' button on clicking the button we will navigate to 'AddSuperHeroes.razor'.
(Step 2)
(Step 3)
In the next article, we are going to implement the Update Operation.
Support Me!
Buy Me A Coffee
PayPal Me
Wrapping Up:
Hopefully, I think this article delivered some useful information on the.NET7 Blazor WebAssembly CRUD sample. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Video Session:
Refer:
Part-2 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Create API Project
Part-3 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Read Operation
Part-3 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Read Operation
Part-5 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Update Operation
Part-6 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Delete Operation
Part-7 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Sorting
Part-6 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Delete Operation
Part-7 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Sorting
Part-8 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Search
Part-9 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Pagination
Part-9 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Pagination
Comments
Post a Comment