Trying to print out property of a virtual list to my view. not sure if accessing it wrong or maybe my database is not structured correctly.
my models:
public class Deck
{
public int id { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
[DisplayName("Card")]
public virtual List<Card> Card { get; set; }
}
public class Card
{
public int Id { get; set; }
public string Name { get; set; }
public int? Atk { get; set; }
public int? Def { get; set; }
public string Desc {get; set;}
public int? Level { get; set; }
public string Type { get; set; }
public string Attribute { get; set; }
[DisplayName("Image")]
public virtual List<Image> Card_Images { get; set; }
public virtual List<Deck> Deck { get; set; }
}
public class Image
{
public int Id { get; set; }
public string image_url { get; set; }
public string image_url_small{ get; set; }
}
My controller action:
public ActionResult Details(int id)
{
var deck = _context.Decks.SingleOrDefault(d => d.id == id);
if (deck == null)
return HttpNotFound();
return View("Details");
}
my view:
@model YGOBuilder.Models.Deck
<div>
<h4>Deck</h4>
<hr />
<dl class="dl-horizontal">
<dd>
@foreach (var m in Model.Card)
{
foreach (var card in m.Card_Images)
{
<td>
<img src=@card.image_url height="300" width="200">
</td>
}
}
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Decks", "Index")
</p>
I've tried fiddling about with the foreach loop, trying different accessing methods but it all seems to trip up on
@foreach (var m in Model.Card)
and throws an 'Object reference not set to an instance of an object.'