This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
TodoItemForm.razor
83 lines (75 loc) · 2.15 KB
/
TodoItemForm.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@using Sample.Models
@inject TodoContext Context
<BSForm>
<BSFormGroup>
<BSLabel For="staticId" class="col-sm-2">Id</BSLabel>
<BSCol SM="10">
<BSInput InputType="InputType.Text" IsReadonly="true" id="staticId" Value="@Todo.Id.ToString()" />
</BSCol>
</BSFormGroup>
<BSFormGroup>
<BSLabel For="inputText" class="col-sm-2">Text</BSLabel>
<BSCol SM="10">
<BSInput InputType="InputType.Text" id="inputText" placeholder="Enter the text of the todo" Value="@Todo.Text" />
</BSCol>
</BSFormGroup>
<div class="form-group row">
<label class="col-sm-2 form-check-label" for="checkDone">Done</label>
<div class="col-sm-10">
<input type="checkbox" class="form-check-input" id="checkDone" @bind="@Todo.IsDone" />
</div>
</div>
<BSCol SM="10" SMOffset="2">
<BSButton Color="Color.Primary" @onclick="@onclick">Submit</BSButton>
</BSCol>
</BSForm>
@functions {
[Parameter] int SelectedId { get; set; }
[Parameter] Action NewTodoAdded { get; set; }
TodoItem Todo { get; set; } = new TodoItem();
protected async override Task OnInitAsync()
{
if (Context.Todos == null)
{
await Context.Initialize();
}
if (SelectedId == 0)
{
Todo = new TodoItem();
}
else
{
SetTodo();
}
}
protected async override Task OnParametersSetAsync()
{
if (Context.Todos == null)
{
await Context.Initialize();
}
SetTodo();
}
void SetTodo()
{
if (Context.Todos == null)
{
return;
}
var query = from todo in Context.Todos
where todo.Id == SelectedId
select todo;
var item = query.FirstOrDefault();
Todo = item != null ? item : new TodoItem();
}
async void onclick()
{
if (Todo.Id == 0)
{
Context.Todos.Add(Todo); //add new todo to the context
}
await Context.SaveChanges();
StateHasChanged();
NewTodoAdded();
}
}