前提・実現したいこと
ASP.NET mvc5 掲示板を作っている初学者です。
現在、タブを押した際に表示されるスレッドのタイトル(DataBase中にはTopicsという名前で定義してます)が種類ごとに絞り込まれるよう機能を実装中に
c#
1return View()
には一つの返り値しかかけないため、複数のLinqの文をcontrollerに書いてもViewに渡すことができない問題に躓いてしまったため質問を投稿しました。
発生している問題・エラーメッセージ
安直すぎるとは自分でも思うのですが、他に手が浮かばずこのように書くと、
Controller
c#
1 // GET: NewTopics 2 public async Task<ActionResult> Index() 3 { 4 5 var topics = db.Topics.Include(t => t.AspNetUsers).Include(t => t.Categories).Include(t => t.Regions); 6 var TurfTopics = db.Topics.Include(t => t.AspNetUsers).Include(t => t.Categories).Include(t => t.Regions).Where(t => t.CategoryId == 1); 7 return View(await topics.ToListAsync(), await topics.ToListAsync()); 8 }
このようなエラーが返ってきました。
引数1:は'System.Collections.Generic<(ソリューション名).Models.Topics>'から'string'へ変換することはできません 引数2:は'System.Collections.Generic<(ソリューション名).Models.Topics>'から'string'へ変換することはできません
該当のソースコード
Controller
C#
1 // GET: NewTopics 2 public async Task<ActionResult> Index() 3 { 4 // DBのTopicsテーブルを全件 5 var topics = db.Topics.Include(t => t.AspNetUsers).Include(t => t.Categories).Include(t => t.Regions); 6 var TurfTopics = db.Topics.Include(t => t.AspNetUsers).Include(t => t.Categories).Include(t => t.Regions).Where(t => t.CategoryId == 1); 7 8 return View(await topics.ToListAsync()); 9 }
View
c#
1 <div class="tabs"> <!-- ここからタブの切り替えボタン --> 2 <input id="all" type="radio" name="tab_item" checked> 3 <label class="tab_item tab_item_all" for="all">ALL</label> 4 <input id="turf" type="radio" name="tab_item"> 5 <input id="otherwise" type="radio" name="tab_item"> 6 <label class="tab_item tab_item_otherwise" for="otherwise">その他</label> 7 <div class="tab_content clearfix" id="all_content"><!-- ここまでタブの切り替えボタン --> 8 9 <table border="0"><!-- ここからカテゴリーを問わず全件表示 --> 10 <tr class="note clearfix"> 11 <th>NO.</th> 12 <th>タイトル</th> 13 <th>レス数</th> 14 <th>HN</th> 15 <th colspan="1">最終コメント日時</th> 16 <th>カテゴリー</th> 17 <th>地域</th> 18 <th>閲覧数</th> 19 </tr><!--/.note--> 20 @foreach (var item in Model) 21 { 22 <tr class="bbs_line clearfix"> 23 <td class="bbs_number">@Html.DisplayFor(modelItem => item.Id)</td> 24 <td class="bbs_title">@Html.ActionLink(item.Title, "Index", new { controller = "Responses", id = item.Id })</td> 25 <td class="bbs_comment_count">@Html.DisplayFor(modelItem => item.ResponseCount)</td> 26 <td class="bbs_name">@Html.DisplayFor(modelItem => item.AspNetUsers.HandleName)</td> 27 <td class="bbs_date">@Html.DisplayFor(modelItem => item.RecentResDate)</td> 28 <td class="bbs_category">@Html.DisplayFor(modelItem => item.Categories.Name)</td> 29 <td class="bbs_region">@Html.DisplayFor(modelItem => item.Regions.Name)</td> 30 <td class="bbs_reading_count">@Html.DisplayFor(modelItem => item.ViewCount)</td> 31 </tr> 32 } 33 </table><!-- ここまでカテゴリーを問わず全件表示 --> 34 35 36 37 38
試したこと
自分なりに調べたところ、Tupleという機能を使えばいいのではないかと考えましたが、Tupleの使い方も理解できなかったため、試したことに加筆できず申し訳ありません。
もし、Controllerのreturn View()の扱いに成功した場合は、foreachの引数のModelにより詳細な名前となるプロパティを加えるのではないかと考えました
c#
1 2 3 @foreach (var item in Model.All) 4 { 5 (省略) 6 } 7 8 @foreach (var item in Model.turf) 9 { 10 (省略) 11 }
補足情報(FW/ツールのバージョンなど)
VS2017
Windows7
.Net Framework 4.6.1
MVC5
回答1件
あなたの回答
tips
プレビュー