/
Case01.cshtml
89 lines (76 loc) · 2.22 KB
/
Case01.cshtml
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
84
85
86
87
88
89
@using System.Linq
@model Project.Models.Day15.ViewModel
<p>Day15/Case01</p>
<form asp-action="Case01"
autocomplete="off"
method="post">
<p>
<label asp-for="OrderDate"></label>
<input type="date"
asp-for="OrderDate" />
</p>
<div>
<label asp-for="Items"></label>
<table>
<thead>
<tr>
<th>
<label asp-for="Items.FirstOrDefault().Name"></label>
</th>
<th>
<label asp-for="Items.FirstOrDefault().Quantity"></label>
</th>
<th>
<button type="button"
onclick="AddItem()">
新增
</button>
</th>
</tr>
</thead>
<tbody id="Items">
@for (int i = 0; i < Model?.Items?.Length; i++)
{
ViewData["OrderItemIndex"] = i;
<partial name="OrderItem"
for="Items[i]"
view-data="ViewData" />
}
</tbody>
</table>
</div>
<p>
<button type="submit">送出</button>
</p>
</form>
<p>
<a asp-action="Index" asp-controller="Home">回上一層</a>
</p>
@section Scripts
{
<script>
window.ItemsCount = @(Model?.Items?.Length ?? 0);
window.AddItemUrl = '@Url.Action("AddOrderItem")';
window.AddItem = function () {
const requestBody = new URLSearchParams();
requestBody.append('index', ItemsCount);
fetch(AddItemUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: requestBody,
})
.then(response => response.text())
.then(data => {
$('#Items').append(data);
ItemsCount++;
})
}
window.DeleteItem = function (e) {
const btnDom = e.target;
const trDom = $(btnDom).parent().parent();
trDom.remove();
}
</script>
}