5.9 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | no-loc | uid | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Build a Blazor todo list app | guardrex | Build a Blazor app step-by-step. | >= aspnetcore-3.0 | riande | mvc | 08/22/2020 |
|
tutorials/build-a-blazor-app |
Build a Blazor todo list app
By Daniel Roth and Luke Latham
This tutorial shows you how to build and modify a Blazor app. You learn how to:
[!div class="checklist"]
- Create a todo list Blazor app project
- Modify Razor components
- Use event handling and data binding in components
- Use routing in a Blazor app
At the end of this tutorial, you'll have a working todo list app.
Prerequisites
Create a todo list Blazor app
-
Create a new Blazor app named
TodoList
in a command shell:dotnet new blazorserver -o TodoList
The preceding command creates a folder named
TodoList
to hold the app. TheTodoList
folder is the root folder of the project. Change directories to theTodoList
folder with the following command:cd TodoList
-
Add a new
Todo
Razor component to the app in thePages
folder using the following command:dotnet new razorcomponent -n Todo -o Pages
[!IMPORTANT] Razor component file names require a capitalized first letter. Open the
Pages
folder and confirm that theTodo
component file name starts with a capital letterT
. The file name should beTodo.razor
. -
In
Pages/Todo.razor
provide the initial markup for the component:@page "/todo" <h3>Todo</h3>
-
Add the
Todo
component to the navigation bar.The
NavMenu
component (Shared/NavMenu.razor
) is used in the app's layout. Layouts are components that allow you to avoid duplication of content in the app.Add a
<NavLink>
element for theTodo
component by adding the following list item markup below the existing list items in theShared/NavMenu.razor
file:<li class="nav-item px-3"> <NavLink class="nav-link" href="todo"> <span class="oi oi-list-rich" aria-hidden="true"></span> Todo </NavLink> </li>
-
Build and run the app by executing the
dotnet run
command in the command shell from theTodoList
folder. Visit the new Todo page to confirm that the link to theTodo
component works. -
Add a
TodoItem.cs
file to the root of the project (theTodoList
folder) to hold a class that represents a todo item. Use the following C# code for theTodoItem
class: -
Return to the
Todo
component (Pages/Todo.razor
):- Add a field for the todo items in an
@code
block. TheTodo
component uses this field to maintain the state of the todo list. - Add unordered list markup and a
foreach
loop to render each todo item as a list item (<li>
).
- Add a field for the todo items in an
-
The app requires UI elements for adding todo items to the list. Add a text input (
<input>
) and a button (<button>
) below the unordered list (<ul>...</ul>
): -
Stop the running app in the command shell. Many command shells accept the keyboard command Ctrl+c to stop an app. Rebuild and run the app with the
dotnet run
command. When theAdd todo
button is selected, nothing happens because an event handler isn't wired up to the button. -
Add an
AddTodo
method to theTodo
component and register it for button selections using the@onclick
attribute. TheAddTodo
C# method is called when the button is selected: -
To get the title of the new todo item, add a
newTodo
string field at the top of the@code
block and bind it to the value of the text input using thebind
attribute in the<input>
element:<input placeholder="Something todo" @bind="newTodo" />
-
Update the
AddTodo
method to add theTodoItem
with the specified title to the list. Clear the value of the text input by settingnewTodo
to an empty string: -
Stop the running app in the command shell. Rebuild and run the app with the
dotnet run
command. Add some todo items to the todo list to test the new code. -
The title text for each todo item can be made editable, and a check box can help the user keep track of completed items. Add a check box input for each todo item and bind its value to the
IsDone
property. Change@todo.Title
to an<input>
element bound to@todo.Title
: -
To verify that these values are bound, update the
<h3>
header to show a count of the number of todo items that aren't complete (IsDone
isfalse
).<h3>Todo (@todos.Count(todo => !todo.IsDone))</h3>
-
The completed
Todo
component (Pages/Todo.razor
): -
Stop the running app in the command shell. Rebuild and run the app with the
dotnet run
command. Add todo items to test the new code.
Next steps
In this tutorial, you learned how to:
[!div class="checklist"]
- Create a todo list Blazor app project
- Modify Razor components
- Use event handling and data binding in components
- Use routing in a Blazor app
Learn about tooling for ASP.NET Core Blazor:
[!div class="nextstepaction"] xref:blazor/tooling