0

I have two models: Order and OrderDetails. Their models:

public class Order
    {   
        public int Id { get; set; }
        public int OrderDetailsId { get; set; }
        public string UserId { get; set; }
        public ApplicationUser User { get; set; }
        public ICollection<OrderDetail> OrderDetails { get; set; }
    }

and:

public class OrderDetail
    {
        public int Id { get; set; }
        public int OrderId { get; set; }
        public Order Order { get; set; }
        public int MenuId { get; set; }
        public Menu Menu { get; set; }
    }

And in the Details View of Order I wanted to show related OrderDetails. Details ActionResult:

public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Order order = db.Orders.Where(o => o.Id == id).Include(o => o.OrderDetails).SingleOrDefault();
            //Order order = db.Orders.Where(o => o.Id == id).Include(o => o.OrderDetails).G;
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

And its view:

@model RestaurantApp.Models.Order

<div>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.User.Email)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.User.Email)
        </dd>
    </dl>
</div>


<table class="table">
    @if (Model.OrderDetails == null)
    {
        <tr>no items found</tr>
    }
    else
    {
        foreach (var item in Model.OrderDetails)
        {
            <tr>
                <td>@item.MenuId</td>
                <td>@item.Quantity</td>
                <td>@item.Price</td>
            </tr>
        }
    }
</table>

And the problem is that I wanted to display Menu.Name instead of MenuId. But when I use Menu.Name it shows System.NullReferenceException: Object reference not set to an instance of an object. How can I solve this problem?

  • What EntityFramework version are you using? EF core has ThenInclude extension method for this – Sir Rufo Mar 23 '20 at 05:38
  • The real question is also a (solved) duplicate => https://stackoverflow.com/questions/10822656/entity-framework-include-multiple-levels-of-properties :o) – Sir Rufo Mar 23 '20 at 05:45

1 Answers1

0

It looks like the Manu property is not being populated on your OrderDetail type. Looking at the Details action, it looks like you're missing to load the referenced data, similar to how you do that for the OrderDetails related entity. Consider updating your query to also populate the Menu entity.

I assume that will be something similar to the below line (not certain about the exact syntax though):

.Include(o => o.OrderDetails).Include(o=>o.OrderDetails.Menu)
Artak
  • 2,481
  • 15
  • 27
  • I have tried many ways of using Menu in Details method but couldn't find that right way. I tried this one too – Gulzhanat Zhumazhanova Mar 23 '20 at 05:32
  • 1
    @GulzhanatZhumazhanova Why do you not ask how to include that nested property? Just asking for "Why is it null?" is a duplicate and gets closed – Sir Rufo Mar 23 '20 at 05:40
  • Thanks for the info. This sparked my curiosity and I think this thread has the answer: https://stackoverflow.com/questions/10822656/entity-framework-include-multiple-levels-of-properties – Artak Mar 23 '20 at 05:45