teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2017/05/08 07:48

投稿

退会済みユーザー
answer CHANGED
@@ -4,4 +4,103 @@
4
4
 
5
5
  なので、自分が提案できる方法は、(1) RouteConfig.cs を変更する、(2) それがダメならコントローラー名/アクションメソッド名(Dashboard/Index)を変更する、(3) それもダメなら Html.ActionLink, Html.RouteLink, Url.Action などを使わないで自力で a 要素、href 属性の文字列を組み立てるコードを書く、(4) それもダメなら href="/?connectionid=ABC" でもなんとかなるように JavaScript のコードを変更する・・・ということぐらいです。
6
6
 
7
- 多分 (3) しかなさそうな気がします。
7
+ 多分 (3) しかなさそうな気がします。
8
+
9
+ 【追伸】
10
+
11
+ 前のスレッド [https://teratail.com/questions/75281](https://teratail.com/questions/75281) に書いてあった質問者さんのコメントの、
12
+
13
+ > (3)a要素を自分で組み立てる->DashboardController.csのIndex(string connectionid)にconnectionidが渡らないというのを検証して、途方に暮れていたところです。
14
+
15
+ というところですが、質問者さんのコードを見ると controller から view に渡される Model から ConnectionId は取得できているように見えます。
16
+
17
+ その想像が当たっていれば、(3) の方法で、例えば以下のような感じで可能なはずです。
18
+
19
+ 以下の例で、Customer というのは AdventureWorksLT サンプルデータベースのテーブルで、CustomerID, Title, FirstName, LastName その他のフィールドを持っています。
20
+
21
+ controller では、EDM を利用して Customer テーブルのレコードを IEnumerable<AdventureWorksLT.Customer> 型のオブジェクトとして取得し、View に Model として渡します。(Take(10) としているのは、全部取得すると長くなりすぎるからです)
22
+
23
+
24
+ ```
25
+ using System;
26
+ using System.Collections.Generic;
27
+ using System.Linq;
28
+ using System.Web;
29
+ using System.Web.Mvc;
30
+ using AdventureWorksLT;
31
+
32
+ namespace Mvc5App.Controllers
33
+ {
34
+ public class HomeController : Controller
35
+ {
36
+ public ActionResult Index()
37
+ {
38
+ AdventureWorksLTEntities db = new AdventureWorksLTEntities();
39
+ var list = (from c in db.Customer
40
+ orderby c.CustomerID ascending
41
+ select c).Take(10);
42
+
43
+ return View(list);
44
+ }
45
+ }
46
+ }
47
+ ```
48
+
49
+ View では controller から Model として渡された IEnumerable<AdventureWorksLT.Customer> 型のオブジェクトから、以下のように a 要素の文字列を作って html コードにレンダリングします。(~/Home... のルート演算子 ~ に注意してください。ASP.NET MVC4 以降でないと以下のような書き方はできないので、@Url.Content("~/Home...) と書く必要があります)
50
+
51
+ ```
52
+ @model IEnumerable<AdventureWorksLT.Customer>
53
+
54
+ @{
55
+ ViewBag.Title = "Home Page";
56
+ }
57
+
58
+ <div>
59
+ <ul>
60
+ @foreach (var item in Model)
61
+ {
62
+ <li>
63
+ <a href="~/Home/Index?connectionid=@item.CustomerID.ToString()">@String.Format("{0} {1} {2}", item.Title, item.FirstName, item.LastName)</a>
64
+ </li>
65
+ }
66
+ </ul>
67
+ </div>
68
+ ```
69
+ 結果、以下の html ソースになります。RouteConfig.cs で controller = "Home", action = "Index" となっていますが、それの影響はなく、href 属性に /Home/Index が設定されています。
70
+
71
+ ```
72
+ <div>
73
+ <ul>
74
+ <li>
75
+ <a href="/Home/Index?connectionid=1">Mr. Orlando Gee</a>
76
+ </li>
77
+ <li>
78
+ <a href="/Home/Index?connectionid=2">Mr. Keith Harris</a>
79
+ </li>
80
+ <li>
81
+ <a href="/Home/Index?connectionid=3">Ms. Donna Carreras</a>
82
+ </li>
83
+ <li>
84
+ <a href="/Home/Index?connectionid=4">Ms. Janet Gates</a>
85
+ </li>
86
+ <li>
87
+ <a href="/Home/Index?connectionid=5">Mr. Lucy Harrington</a>
88
+ </li>
89
+ <li>
90
+ <a href="/Home/Index?connectionid=6">Ms. Rosmarie Carroll</a>
91
+ </li>
92
+ <li>
93
+ <a href="/Home/Index?connectionid=7">Mr. Dominic Gash</a>
94
+ </li>
95
+ <li>
96
+ <a href="/Home/Index?connectionid=10">Ms. Kathleen Garza</a>
97
+ </li>
98
+ <li>
99
+ <a href="/Home/Index?connectionid=11">Ms. Katherine Harding</a>
100
+ </li>
101
+ <li>
102
+ <a href="/Home/Index?connectionid=12">Mr. Johnny Caprio</a>
103
+ </li>
104
+ </ul>
105
+ </div>
106
+ ```