I try to expand the server side blazor todo app.
I would like to
This gist contains my attempt to expand the todo app
Adding a list of people inside a select field
<select>
@foreach (var person in people)
{
<option value="person">@person</option>
}
</select>
Two things are not working
todoItem
inside AddTodo()
todoItem
for the event onKeyUp == Enter
The method
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo, Responsible = person});
newTodo = string.Empty;
}
}
Source code of my Todo.razor gist
The cause for why is onkeyup not firing?
was a silly mistake; the event handler was attached to the wrong input. This seems to work
<input placeholder="Something todo" @bind="newTodo" @onkeyup="AddTodoOnEnter" />
To get a value from a selected option from a select-tag to your code you bind the variable @newPerson
on the select
-tag.
<select @bind="newPerson">
@foreach (var person in people)
{
<option value="@person" >@person.name (@person.id)</option>
}
</select>
You can access it inside the @code section with
@code {
// named tuple list
private List<(int id, string name)> people = new List<(int id, string name)>()
{
(1, "Tom"), (2, "Luise"), (3,"Jane")
};
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo, Responsible = "who " + newPerson });
newTodo = string.Empty;
}
}
I updated my gist - you can see the old code next the revision 4 side by side here