foreach error, re: Add enrollments to the Details view

Dean Everhart 1,366 Reputation points
2023-05-22T15:28:12.4533333+00:00

Project Description:
Three Property Sets: A, B, and C
A and B with one-to-many relationships with **C

Error: when trying to view A, Details view.

User's image


Tutorial: Add enrollments to the Details view

Environment: Net Core 6 MVC, Visual Studio Community 2022 (64 bit), WIndows 11


**Code Follows:

Context**

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ViewModel.Models;

namespace ViewModel.Data
{
    public class ViewModelContext : DbContext
    {
        public ViewModelContext (DbContextOptions<ViewModelContext> options)
            : base(options)
        {
        }

        public DbSet<ViewModel.Models.A> A { get; set; } = default!;

        public DbSet<ViewModel.Models.B>? B { get; set; }

        public DbSet<ViewModel.Models.C>? C { get; set; }
    }
}

A

namespace ViewModel.Models
{
    public class A
    {
        public int Id { get; set; }

        public string? One { get; set; }

        public string? Two { get; set; }

        public string? Three { get; set; }

        // _______________________________________

        public IEnumerable<C>? C { get; set; }
    }
}

B

namespace ViewModel.Models
{
    public class B
    {
        public int Id { get; set; }

        public string? One { get; set; }

        public string? Two { get; set; }

        public string? Three { get; set; }

        // _______________________________________

        public IEnumerable<C>? C { get; set; }
    }
}

C

using System.Security.Cryptography.Xml;

namespace ViewModel.Models
{
    public class C
    {
        public int Id { get; set; }

        public int AId { get; set; }

        public int BId { get; set; }

        // _______________________________________

        public A? A { get; set; }
        public B? B { get; set; }
    }
}

View.A

User's image

Controller, Details Action Method

        // GET: A/Details/5
        public async Task<IActionResult> Details(int? id)
    {
        if (id == null || _context.A == null)
        {
            return NotFound();
        }

        var a = await _context.A
            .FirstOrDefaultAsync(m => m.Id == id);
        if (a == null)
        {
            return NotFound();
        }

        return View(a);
    }

Build Results

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Run web app, Select A, Details view

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
3,150 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
852 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 36,026 Reputation points
    2023-05-22T15:51:26.5233333+00:00

    you need to use .Include() to load navigation properties. see tutorial.

    0 comments No comments