0

I have a table named Invoice and it has a child table items in the consignment note. And there is a table of goods.

In MVC 5 it was possible to make the following construction on the page:

@foreach (var item in Model)
{
<tr>
    <td>
    @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @foreach (var items in item.ListProdukts)
        {

            @Html.DisplayFor(modelItem => items.Produkts.ProduktName)
        }

But this does not work in ASP.NET Core.

Model:

namespace Proba3.Models
{
    public class Invoice
{
    public int InvoiceId { get; set; }
    public string InvoiceName { get; set; }
    public ICollection<ListProdukt> ListProdukts { get; set; }

}
}

namespace Proba3.Models
{
    public class ListProdukt
    {
        public int ListProduktId { get; set; }
        public int ProduktId { get; set; }
        public virtual Produkt Produkts { get; set; }

        public int InvoiceId { get; set; }
        public virtual Invoice Invoices { get; set; }
    }
}

 namespace Proba3.Models
 {
     public class Produkt
     {
        public int ProduktId { get; set; }
        public string ProduktName { get; set; }
        public ICollection<ListProdukt> ListProdukts { get; set; }
    }
}

Controllers:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Proba3.Data;
using Proba3.Models;

namespace Proba3.Controllers
{
public class InvoicesController : Controller
{


    private readonly ApplicationDbContext _context;

    public InvoicesController(ApplicationDbContext context)
    {
        _context = context;    
    }

    // GET: Invoices
    public async Task<IActionResult> Index()
    {
       var applicationDbContext = _context.Invoice.Include(c => c.ListProdukts);

        return View(await applicationDbContext.ToListAsync());
    }

Views:

@model IEnumerable<Proba3.Models.Invoice>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.InvoiceName)
        </th>
        <th></th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.InvoiceName)
            </td>
            <td>
            @foreach (var items in item.ListProdukts)
            {
                <p>@Html.DisplayFor(modelItem => items.ProduktId)</p>
                <p>@Html.DisplayFor(modelItem => items.Produkts.ProduktName)</p>
            }
            </td>
            <td>

Why not show ProduktName?

blakcat
  • 526
  • 1
  • 4
  • 12
  • In asp core this will work too. Edit: It's look like you have not initialized list of produkts. Can you show a code in controller ? – Ridikk12 Sep 18 '16 at 08:51
  • Don't post it as comment, update your answer. Its pretty hard to read – Tseng Sep 18 '16 at 09:35
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ondrej Tucny Sep 18 '16 at 09:40

1 Answers1

0

The answer is given on github.

It turns out that ASP.NET Core now does not add child elements by default. Details can be read in here: docs.asp.net

In my case, it is necessary to add to the Controller:

in a row:

var applicationDbContext = _context.Invoice.Include (c => c.ListProdukts) .ThenInclude (c => c.Produkts);

this:

.ThenInclude (C => c.Produkts);
blakcat
  • 526
  • 1
  • 4
  • 12