回答編集履歴
1
追記
test
CHANGED
@@ -11,3 +11,69 @@
|
|
11
11
|
|
12
12
|
チュートリアル: 並べ替え、フィルター処理、ページングを追加する - ASP.NET MVC と EF Core
|
13
13
|
https://docs.microsoft.com/ja-jp/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-6.0
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
**【追記】**
|
18
|
+
|
19
|
+
下の 2022/09/13 11:53 の私のコメントで「回答欄に追記します」と書いた件です。
|
20
|
+
|
21
|
+
> テキストボックスはSearchStringでcontrollerに値を受け渡しているという解釈であっていますか??
|
22
|
+
|
23
|
+
そうです。もう少し詳しく説明します。
|
24
|
+
|
25
|
+
チュートリアルはビュー index.cshtml に以下のコードを追加してますが、それはやったんですよね? そして、アプリを実行して index ページを表示するとテキストボックスとボタンが表示されるのですよね?
|
26
|
+
|
27
|
+
```
|
28
|
+
@using (Html.BeginForm())
|
29
|
+
{
|
30
|
+
<p>
|
31
|
+
Find by name: @Html.TextBox("SearchString")
|
32
|
+
<input type="submit" value="Search" /></p>
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
であれば、そのテキストボックスに例えば abc と入力してボタンをクリックすると、
|
37
|
+
|
38
|
+
SearchString=abc
|
39
|
+
|
40
|
+
というクエリ文字列が index ページの url に追加されてサーバーに GET 要求されます。
|
41
|
+
|
42
|
+
|
43
|
+
> データベースに接続し、index.cshtmlで一覧画面が表示されています。
|
44
|
+
|
45
|
+
ということですから、現状の Index アクションメソッドは以下のようになっていると想像してます。
|
46
|
+
|
47
|
+
```C#
|
48
|
+
public ActionResult Index()
|
49
|
+
{
|
50
|
+
|
51
|
+
// SELECT * FROM Bihin で全レコードを DataTable に取得
|
52
|
+
// DataTable から List<BihinDB> modelList を生成
|
53
|
+
|
54
|
+
return View(modelList);
|
55
|
+
}
|
56
|
+
```
|
57
|
+
|
58
|
+
そのアクションメソッドで送信されてきた SearchString=abc を受け取るためにはチュートリアルの「[Index メソッドにフィルター機能を追加する](https://docs.microsoft.com/ja-jp/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application#add-filtering-functionality-to-the-index-method)」セクションに書いてあるように、Index メソッドの引数に searchString を追加します。(注: アクションメソッドの引数は大文字小文字の区別はされません)。
|
59
|
+
|
60
|
+
そして、チュートリアルのように Index アクションメソッドで searchString の有無を調べて処理を分ければよさそうです。
|
61
|
+
|
62
|
+
|
63
|
+
```C#
|
64
|
+
public ViewResult Index(string searchString)
|
65
|
+
{
|
66
|
+
if (!String.IsNullOrEmpty(searchString))
|
67
|
+
{
|
68
|
+
// SELECT * FROM Bihin LIKE searchString で検索したレコードを DataTable に取得
|
69
|
+
}
|
70
|
+
else
|
71
|
+
{
|
72
|
+
// SELECT * FROM Bihin で全レコードを DataTable に取得
|
73
|
+
}
|
74
|
+
|
75
|
+
// DataTable から List<BihinDB> modelList を生成
|
76
|
+
|
77
|
+
return View(modelList);
|
78
|
+
}
|
79
|
+
```
|