Witam,
Tworzę niewielki projekt w MVC4, ale mam problem z relacjami wiele do wielu. Mianowicie utworzyłem bazę danych która wygląda tak:
Oczywiście między tymi tabelami znajduje się tabela pomocnicza zawierająca ID_AN i ID_GE jako klucz podstawowy.
Stworzyłem o to taki model:
namespace AnimeWeb.Models
{
using System;
using System.Collections.Generic;
public partial class DetailsModel
{
public ANIME MyANIME { get; set; }
public GENRES MyGENRES { get; set; }
}
}
Następnie utworzyłem controller z ActionResult wyglądającym tak:
public ActionResult Details(int id)
{
using (var db = new MainDatabaseEntities())
{
DetailsModel viewModel = new DetailsModel();
viewModel.MyANIME = db.ANIME.FirstOrDefault(a => a.ID_AN == id);
viewModel.MyCHARACTER = db.GENRES.FirstOrDefault(a => a.ID_CH == id);
return View(viewModel);
}
}
i na koniec do tego utworzyłem View:
@model AnimeWeb.Models.DetailsModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>DetailsModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.SMALL_PIC)
</div>
<div>
<img src="../../Images/Anime/@Html.DisplayFor(model => model.MyANIME.SMALL_PIC)" height="314" width="225">
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.TITLE_OR)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.TITLE_OR)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.TITLE_EN)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.TITLE_EN)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.TYPE)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.TYPE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.EPISODES)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.EPISODES)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.AIRED_START)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.AIRED_START)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.AIRED_END)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.AIRED_END)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.SYNOPSIS)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.SYNOPSIS)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.RATING)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.RATING)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.OP_THEME)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.OP_THEME)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyANIME.ED_THEME)
</div>
<div>
@Html.DisplayFor(model => model.MyANIME.ED_THEME)
</div>
</fieldset>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
Dane znajdujące się u góry wyświetlane są prawidłowo, ale za chiny teraz nie wiem jak wyświetlić wszystkie Gatunki przypisane do tego Anime. Przypuszczam, że muszę użyć pętli foreach, ale wtedy dostaję błąd, że model nie jest IEnumerable i nie wiem jak sobie z tym poradzić. Jakieś rady?