Starts in:
1 DAY
11 HRS
32 MIN
5 SEC
Starts in:
1 D
11 H
32 M
5 S

There are multiple versions of this document. Pick the options that suit you best.

UI
Database

Web Application Development Tutorial - Part 3: Creating, Updating and Deleting Books

About This Tutorial

In this tutorial series, you will build an ABP based web application named Acme.BookStore. This application is used to manage a list of books and their authors. It is developed using the following technologies:

  • MongoDB as the database provider.
  • Blazor WebAssembly as the UI Framework.

This tutorial is organized as the following parts;

Download the Source Code

This tutorial has multiple versions based on your UI and Database preferences. We've prepared a few combinations of the source code to be downloaded:

If you encounter the "filename too long" or "unzip" error on Windows, please see this guide.

Creating a New Book

In this section, you will learn how to create a new modal dialog form to create a new book. Since we've inherited from the AbpCrudPageBase, we only need to develop the view part.

Add "New Button" Button

Open the Books.razor.cs and create an override for SetToolbarItemsAsync method:

    protected override ValueTask SetToolbarItemsAsync()
    {
        Toolbar.AddButton(L["NewBook"],
            OpenCreateModalAsync,
            IconName.Add);

        return base.SetToolbarItemsAsync();
    }

This will add a "New book" button to the right side of the toolbar:

blazor-add-book-button

Now, we can add a modal that will be opened when we click to the button.

Book Creation Modal

Open the Books.razor and add the following code to the end of the page:

<Modal @ref="@CreateModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <Form>
            <ModalHeader>
                <ModalTitle>@L["NewBook"]</ModalTitle>
                <CloseButton Clicked="CloseCreateModalAsync"/>
            </ModalHeader>
            <ModalBody>
                <Validations @ref="@CreateValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["Name"]</FieldLabel>
                            <TextEdit @bind-Text="@NewEntity.Name">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Field>
                        <FieldLabel>@L["Type"]</FieldLabel>
                        <Select TValue="BookType" @bind-SelectedValue="@NewEntity.Type">
                            @foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
                            {
                                <SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
                                    @L[$"Enum:BookType.{bookTypeValue}"]
                                </SelectItem>
                            }
                        </Select>
                    </Field>
                    <Field>
                        <FieldLabel>@L["PublishDate"]</FieldLabel>
                        <DateEdit TValue="DateTime" @bind-Date="NewEntity.PublishDate"/>
                    </Field>
                    <Field>
                        <FieldLabel>@L["Price"]</FieldLabel>
                        <NumericEdit TValue="float" @bind-Value="NewEntity.Price"/>
                    </Field>
                </Validations>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Secondary"
                        Clicked="CloseCreateModalAsync">@L["Cancel"]</Button>
                <Button Color="Color.Primary"
                        Type="@ButtonType.Submit"
                        PreventDefaultOnSubmit="true"
                        Clicked="CreateEntityAsync">@L["Save"]</Button>
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>

This code requires a service; Inject the AbpBlazorMessageLocalizerHelper<T> at the top of the file, just before the @inherits... line:

@using Volo.Abp.AspNetCore.Components.Web;

@inject AbpBlazorMessageLocalizerHelper<BookStoreResource> LH
  • The form implements validation and the AbpBlazorMessageLocalizerHelper is used to simply localize the validation messages.

  • CreateModal object, CloseCreateModalAsync and CreateEntityAsync method are defined by the base class. See the Blazorise documentation if you want to understand the Modal and the other components.

That's all. Run the application and try to add a new book:

blazor-new-book-modal

Updating a Book

Editing a books is similar to the creating a new book.

Actions Dropdown

Open the Books.razor and add the following DataGridEntityActionsColumn section inside the DataGridColumns as the first item:

<DataGridEntityActionsColumn TItem="BookDto" @ref="@EntityActionsColumn">
    <DisplayTemplate>
        <EntityActions TItem="BookDto" EntityActionsColumn="@EntityActionsColumn">
            <EntityAction TItem="BookDto"
                          Text="@L["Edit"]"
                          Clicked="() => OpenEditModalAsync(context)" />
        </EntityActions>
    </DisplayTemplate>
</DataGridEntityActionsColumn>
  • OpenEditModalAsync is defined in the base class which takes the entity (book) to edit.

DataGridEntityActionsColumn component is used to show an "Actions" dropdown for each row in the DataGrid.

blazor-edit-book-action

Edit Modal

We can now define a modal to edit the book. Add the following code to the end of the Books.razor page:

<Modal @ref="@EditModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <Form>
            <ModalHeader>
                <ModalTitle>@EditingEntity.Name</ModalTitle>
                <CloseButton Clicked="CloseEditModalAsync"/>
            </ModalHeader>
            <ModalBody>
                <Validations @ref="@EditValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["Name"]</FieldLabel>
                            <TextEdit @bind-Text="@EditingEntity.Name">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Field>
                        <FieldLabel>@L["Type"]</FieldLabel>
                        <Select TValue="BookType" @bind-SelectedValue="@EditingEntity.Type">
                            @foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
                            {
                                <SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
                                    @L[$"Enum:BookType.{bookTypeValue}"]
                                </SelectItem>
                            }
                        </Select>
                    </Field>
                    <Field>
                        <FieldLabel>@L["PublishDate"]</FieldLabel>
                        <DateEdit TValue="DateTime" @bind-Date="EditingEntity.PublishDate"/>
                    </Field>
                    <Field>
                        <FieldLabel>@L["Price"]</FieldLabel>
                        <NumericEdit TValue="float" @bind-Value="EditingEntity.Price"/>
                    </Field>
                </Validations>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Secondary"
                        Clicked="CloseEditModalAsync">@L["Cancel"]</Button>
                <Button Color="Color.Primary"
                        Type="@ButtonType.Submit"
                        PreventDefaultOnSubmit="true"
                        Clicked="UpdateEntityAsync">@L["Save"]</Button>
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>

AutoMapper Configuration

The base AbpCrudPageBase uses the object to object mapping system to convert an incoming BookDto object to a CreateUpdateBookDto object. So, we need to define the mapping.

Open the BookStoreBlazorAutoMapperProfile inside the Acme.BookStore.Blazor project and change the content as the following:


using Acme.BookStore.Books;
using AutoMapper;

namespace Acme.BookStore.Blazor;

public class BookStoreBlazorAutoMapperProfile : Profile
{
    public BookStoreBlazorAutoMapperProfile()
    {
        CreateMap<BookDto, CreateUpdateBookDto>();
    }
}
  • We've just added the CreateMap<BookDto, CreateUpdateBookDto>(); line to define the mapping.

Test the Editing Modal

You can now run the application and try to edit a book.

blazor-edit-book-modal

Deleting a Book

Open the Books.razor page and add the following EntityAction under the "Edit" action inside the EntityActions:

<EntityAction TItem="BookDto"
              Text="@L["Delete"]"
              Clicked="() => DeleteEntityAsync(context)"
              ConfirmationMessage="() => GetDeleteConfirmationMessage(context)" />
  • DeleteEntityAsync is defined in the base class that deletes the entity by performing a call to the server.
  • ConfirmationMessage is a callback to show a confirmation message before executing the action.
  • GetDeleteConfirmationMessage is defined in the base class. You can override this method (or pass another value to the ConfirmationMessage parameter) to customize the localization message.

blazor-edit-book-action

Run the application and try to delete a book.

Full CRUD UI Code

Here the complete code to create the book management CRUD page, that has been developed in the last two parts:

@page "/books"
@using Volo.Abp.Application.Dtos
@using Acme.BookStore.Books
@using Acme.BookStore.Localization
@using Volo.Abp.AspNetCore.Components.Web.Theming.Layout
@using Microsoft.Extensions.Localization
@using Volo.Abp.AspNetCore.Components.Web;
@inject IStringLocalizer<BookStoreResource> L
@inject AbpBlazorMessageLocalizerHelper<BookStoreResource> LH
@inherits AbpCrudPageBase<IBookAppService, BookDto, Guid, PagedAndSortedResultRequestDto, CreateUpdateBookDto>

<CascadingValue Value="this">
    @* ************************* PAGE HEADER ************************* *@
    <PageHeader Title="@L["Books"]" BreadcrumbItems="@BreadcrumbItems" Toolbar="@Toolbar">
    </PageHeader>

    <Card>
        <CardBody>
            <DataGrid TItem="BookDto"
                      Data="Entities"
                      ReadData="OnDataGridReadAsync"
                      CurrentPage="CurrentPage"
                      TotalItems="TotalCount"
                      ShowPager="true"
                      PageSize="PageSize">
                <DataGridColumns>
                    <DataGridEntityActionsColumn TItem="BookDto" @ref="@EntityActionsColumn">
                        <DisplayTemplate>
                            <EntityActions TItem="BookDto" EntityActionsColumn="@EntityActionsColumn">
                                <EntityAction TItem="BookDto"
                                              Text="@L["Edit"]"
                                              Clicked="() => OpenEditModalAsync(context)" />
                                <EntityAction TItem="BookDto"
                                              Text="@L["Delete"]"
                                              Clicked="() => DeleteEntityAsync(context)"
                                              ConfirmationMessage="() => GetDeleteConfirmationMessage(context)" />
                            </EntityActions>
                        </DisplayTemplate>
                    </DataGridEntityActionsColumn>
                    <DataGridColumn TItem="BookDto"
                                    Field="@nameof(BookDto.Name)"
                                    Caption="@L["Name"]"></DataGridColumn>
                    <DataGridColumn TItem="BookDto"
                                    Field="@nameof(BookDto.Type)"
                                    Caption="@L["Type"]">
                        <DisplayTemplate>
                            @L[$"Enum:BookType.{(int)context.Type}"]
                        </DisplayTemplate>
                    </DataGridColumn>
                    <DataGridColumn TItem="BookDto"
                                    Field="@nameof(BookDto.PublishDate)"
                                    Caption="@L["PublishDate"]">
                        <DisplayTemplate>
                            @context.PublishDate.ToShortDateString()
                        </DisplayTemplate>
                    </DataGridColumn>
                    <DataGridColumn TItem="BookDto"
                                    Field="@nameof(BookDto.Price)"
                                    Caption="@L["Price"]">
                    </DataGridColumn>
                    <DataGridColumn TItem="BookDto"
                                    Field="@nameof(BookDto.CreationTime)"
                                    Caption="@L["CreationTime"]">
                        <DisplayTemplate>
                            @context.CreationTime.ToLongDateString()
                        </DisplayTemplate>
                    </DataGridColumn>
                </DataGridColumns>
            </DataGrid>
        </CardBody>
    </Card>

    <Modal @ref="@CreateModal">
        <ModalBackdrop />
        <ModalContent IsCentered="true">
            <Form>
                <ModalHeader>
                    <ModalTitle>@L["NewBook"]</ModalTitle>
                    <CloseButton Clicked="CloseCreateModalAsync" />
                </ModalHeader>
                <ModalBody>
                    <Validations @ref="@CreateValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                        <Validation MessageLocalizer="@LH.Localize">
                            <Field>
                                <FieldLabel>@L["Name"]</FieldLabel>
                                <TextEdit @bind-Text="@NewEntity.Name">
                                    <Feedback>
                                        <ValidationError />
                                    </Feedback>
                                </TextEdit>
                            </Field>
                        </Validation>
                        <Field>
                            <FieldLabel>@L["Type"]</FieldLabel>
                            <Select TValue="BookType" @bind-SelectedValue="@NewEntity.Type">
                                @foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
                                {
                                    <SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
                                        @L[$"Enum:BookType.{bookTypeValue}"]
                                    </SelectItem>
                                }
                            </Select>
                        </Field>
                        <Field>
                            <FieldLabel>@L["PublishDate"]</FieldLabel>
                            <DateEdit TValue="DateTime" @bind-Date="NewEntity.PublishDate" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["Price"]</FieldLabel>
                            <NumericEdit TValue="float" @bind-Value="NewEntity.Price" />
                        </Field>
                    </Validations>
                </ModalBody>
                <ModalFooter>
                    <Button Color="Color.Secondary"
                            Clicked="CloseCreateModalAsync">
                        @L["Cancel"]
                    </Button>
                    <Button Color="Color.Primary"
                            Type="@ButtonType.Submit"
                            PreventDefaultOnSubmit="true"
                            Clicked="CreateEntityAsync">
                        @L["Save"]
                    </Button>
                </ModalFooter>
            </Form>
        </ModalContent>
    </Modal>

    <Modal @ref="@EditModal">
        <ModalBackdrop />
        <ModalContent IsCentered="true">
            <Form>
                <ModalHeader>
                    <ModalTitle>@EditingEntity.Name</ModalTitle>
                    <CloseButton Clicked="CloseEditModalAsync" />
                </ModalHeader>
                <ModalBody>
                    <Validations @ref="@EditValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                        <Validation MessageLocalizer="@LH.Localize">
                            <Field>
                                <FieldLabel>@L["Name"]</FieldLabel>
                                <TextEdit @bind-Text="@EditingEntity.Name">
                                    <Feedback>
                                        <ValidationError />
                                    </Feedback>
                                </TextEdit>
                            </Field>
                        </Validation>
                        <Field>
                            <FieldLabel>@L["Type"]</FieldLabel>
                            <Select TValue="BookType" @bind-SelectedValue="@EditingEntity.Type">
                                @foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
                                {
                                    <SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
                                        @L[$"Enum:BookType.{bookTypeValue}"]
                                    </SelectItem>
                                }
                            </Select>
                        </Field>
                        <Field>
                            <FieldLabel>@L["PublishDate"]</FieldLabel>
                            <DateEdit TValue="DateTime" @bind-Date="EditingEntity.PublishDate" />
                        </Field>
                        <Field>
                            <FieldLabel>@L["Price"]</FieldLabel>
                            <NumericEdit TValue="float" @bind-Value="EditingEntity.Price" />
                        </Field>
                    </Validations>
                </ModalBody>
                <ModalFooter>
                    <Button Color="Color.Secondary"
                            Clicked="CloseEditModalAsync">
                        @L["Cancel"]
                    </Button>
                    <Button Color="Color.Primary"
                            Type="@ButtonType.Submit"
                            PreventDefaultOnSubmit="true"
                            Clicked="UpdateEntityAsync">
                        @L["Save"]
                    </Button>
                </ModalFooter>
            </Form>
        </ModalContent>
    </Modal>
</CascadingValue>

The Next Part

See the next part of this tutorial.

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More