0

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.'

dros
  • 1,217
  • 2
  • 15
  • 31
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – SᴇM Sep 16 '19 at 12:21
  • You are not passing the model to the view. Use `return View("Details", deck);` – Chetan Sep 16 '19 at 12:25
  • You're binding the wrong model to your view. You have "@model YGOBuilder.Models.Deck" yet your foreach is looping through Model.Card. Change it to Model.Deck or change the model being binded to YGOBuilder.Models.Card. If both are in different classes, use a view model to flatten it out. – user10728126 Sep 16 '19 at 12:35
  • @ChetanRanpariya are you referring to my controller? – dros Sep 16 '19 at 12:41
  • Yes..I am referring to your controller action – Chetan Sep 16 '19 at 12:45
  • sorry, manipulating the controller doesnt work, still null error – dros Sep 16 '19 at 12:51
  • @user10728126 this doesnt work, it requires Deck by the looks of it. Seeing as Images are a class with Card, and Deck contains Card. should it not pull through anyway regardless of a view model? – dros Sep 16 '19 at 12:52
  • Are you sure that you are calling the website with an `id` end of it? – darcane Sep 16 '19 at 12:54
  • @AndyStav You need to put a break point in View and debut the application. With the code change I suggested at least Model property of the View should not be NULL. You need to debug and check what exactly is null. Is the Model Null or the Model.Card null? – Chetan Sep 16 '19 at 12:56
  • You need to initialize the Card collection. Create a constructor for class Deck and inside that initialize your collection: List Card = new List(); – user10728126 Sep 16 '19 at 12:58
  • @ChetanRanpariya thank you so much you were completely correct, silly mistake on my part. thanks very much – dros Sep 16 '19 at 13:05

1 Answers1

1

You are getting null reference exception because your Model is null. You need to pass the model to your view. Since your view and action names are the same you can return your view with the model like this : return View(deck);

public ActionResult Details(int id)
{

    var deck = _context.Decks.SingleOrDefault(d => d.id == id);

    if (deck == null)
        return HttpNotFound();

    return View(deck);
}
darcane
  • 473
  • 3
  • 14