Introduction
In this blog, we will learn about how to create a .NET Core 6.0 Blazor WebAssembly application that can do CRUD operations using EF Core.
Prerequisites
- Visual Studio 2022 installed,
- Microsoft SQL Server 18,
- Basics of Asp.Net Web, Entity Framework
Source Code
This source code is publically available on GitHub link
Step1
Open Visual Studio 2022 Click on Create a New Project, choose a Blazor WebAssembly.

Click next, then setup your Project Name, then click next.
Then Choose Framework - .Net 6.0 (Long Term Support), check on checkbox called ASP.Net Core hosted, then click Create.
Step 2
A template of Blazor Application has been created, you can run and see the following output.

Our solution will have three projects Client, Server and Shared.
On Client project we will place the client-side razor pages and client-side libraries.
On Server project we will place Controller, Service, Repository, Entity class, DbContext for EF etc.
On Shared project we will place a DTO/View model for client-side pages.
Step 3
On Shared project create a class called PersonViewModel

Inside PersonViewModel place the following code:
public class PersonViewModel {
public int Id {
get;
set;
}
[Required]
[Display(Name = "First Name")]
public string FirstName {
get;
set;
}
[Required]
[Display(Name = "Last Name")]
public string LastName {
get;
set;
}
[Required]
[Display(Name = "Email")]
public string Email {
get;
set;
}
[Required]
[Display(Name = "Mobile Number")]
public string MobileNo {
get;
set;
}
}
Step 4
Inside Shared folder of client project there is a NavMenu.razor page edit this page to add NavMenu for Person.

Add the following code for new menu option Personal Info :
<div class="nav-item px-3">
<NavLink class="nav-link" href="personList">
<span class="oi oi-list-rich" aria-hidden="true"></span> Personal Info
</NavLink>
</div>
Right Click on Pages > Add > Razor component.
Let’s create a component PersonList.razor
Inside PersonList.razor place the following code:
@page "/personlist"
@using BlazorCRUDApp.Shared
@inject HttpClient _httpClient
<h1>Personal Detail</h1>
<div>
<a href="/addperson"> Create New Person</a>
@if (personList != null)
{
<table class="table-bordered">
<thead>
<tr>
<th width = "30%">Name</th>
<th width = "20%">Email</th>
<th width = "20%">Mobile No</th>
<th width = "30%">Action</th>
</tr>
</thead>
<tbody>
@foreach (var p in personList)
{
<tr>
<td>
@p.FirstName @p.LastName
</td>
<td>@p.Email</td>
<td>@p.MobileNo</td>
<td>
<a href="/editperson/@p.Id">Edit</a> |
<a href="/deleteperson/@p.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
}
</div>
@code {
List<PersonViewModel> personList = new List<PersonViewModel>();
protected override async Task OnInitializedAsync()
{
var response = await _httpClient.GetAsync("api/person");
response.EnsureSuccessStatusCode();
personList = await response.Content.ReadFromJsonAsync<List<PersonViewModel>>();
}
}
Let’s create another component AddPerson.razor.
Inside AddPerson.razor place the following code:
@page "/addperson"
@using BlazorCRUDApp.Shared
@inject HttpClient Http
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
<h2>Add Person</h2>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="FirstName"> First Name *</label>
<input form="FirstName" class="form-control" @bind="@person.FirstName" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="LastName"> Last Name *</label>
<input form="LastName" class="form-control" @bind="@person.LastName" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="Email"> Email *</label>
<input form="Email" class="form-control" @bind="@person.Email" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="MobileNo"> Mobile No *</label>
<input form="mobileNo" class="form-control" @bind="@person.MobileNo" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" @onclick="@Save" value="Save" />
<input type="button" class="btn" @onclick="@Cancel" value="Cancel" />
</div>
</div>
</div>
@code {
PersonViewModel person = new PersonViewModel();
protected override async Task OnInitializedAsync()
{
}
protected async Task Save()
{
var response = await Http.PostAsJsonAsync("api/Person", @person);
PersonViewModel personResponse = await response.Content.ReadFromJsonAsync<PersonViewModel>();
if (personResponse?.Id > 0)
{
await JsRuntime.InvokeVoidAsync("alert", "Saved Successfully!");
NavigationManager.NavigateTo("personlist");
}
}
void Cancel()
{
NavigationManager.NavigateTo("personlist");
}
}
Let’s create another component EditPerson.razor.
Inside EditPerson.razor place the following code:
@page "/editperson/{Id}"
@using BlazorCRUDApp.Shared
@inject HttpClient Http
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
<h2>Edit Person</h2>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="FirstName"> First Name *</label>
<input form="FirstName" class="form-control" @bind="@person.FirstName" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="LastName"> Last Name *</label>
<input form="LastName" class="form-control" @bind="@person.LastName" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="Email"> Email *</label>
<input form="Email" class="form-control" @bind="@person.Email" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="MobileNo"> Mobile No *</label>
<input form="mobileNo" class="form-control" @bind="@person.MobileNo" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" @onclick="@Save" value="Save" />
<input type="button" class="btn" @onclick="@Cancel" value="Cancel" />
</div>
</div>
</div>
@code {
[Parameter]
public string Id{ get; set; }
PersonViewModel person = new PersonViewModel();
protected override async Task OnInitializedAsync()
{
person = await Http.GetFromJsonAsync<PersonViewModel>("api/person/" + Id);
}
protected async Task Save()
{
var response = await Http.PutAsJsonAsync("api/Person/" + Id, @person);
bool personResponse = await response.Content.ReadFromJsonAsync<bool>();
if (personResponse)
{
await JsRuntime.InvokeVoidAsync("alert", "Updated Successfully!");
NavigationManager.NavigateTo("personlist");
}
}
void Cancel()
{
NavigationManager.NavigateTo("personlist");
}
}








spiro bPosted Jul 16, 2022, 5:15 PM
Awesome!. Great man!
David MendozaPosted Jun 26, 2022, 7:40 AM
Awesome stuff!!
Barry SchneiderPosted Jun 17, 2022, 4:33 PM
What is the benefit of creating two services instead of calling the repository service directly from the API?
jose loraPosted Jan 26, 2022, 8:23 PM
EXCELENT WORK, FROM DOMINICAN REPUBLIC