Reputation: 92
The error: My Controller where the exception is trown:
public class UitgiftesController : Controller
{
private readonly ApplicationDbContext _context;
private UserManager<Medewerker> _userManager;
//class constructor
public UitgiftesController(UserManager<Medewerker> userManager)
{
_userManager = userManager;
}
// GET: Uitgiftes
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.Uitgiftes.Include(u => u.Onderdelen);
return View(await applicationDbContext.ToListAsync());
}
// GET: Uitgiftes/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var uitgiftes = await _context.Uitgiftes
.Include(u => u.Onderdelen)
.FirstOrDefaultAsync(m => m.Id == id);
if (uitgiftes == null)
{
return NotFound();
}
return View(uitgiftes);
}
// GET: Uitgiftes/Create
public IActionResult Create()
{
ViewData["OnderdelenId"] = new SelectList(_context.Set<Onderdelen>(), "Id", "Naam");
return View();
}
// POST: Uitgiftes/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Tijdstip,GewichtKg,Prijs,OnderdelenapparaatId,OnderdelenId,MedewerkerId")] Uitgiftes uitgiftes)
{
if (ModelState.IsValid)
{
_context.Add(uitgiftes);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["OnderdelenId"] = new SelectList(_context.Set<Onderdelen>(), "Id", "Naam", uitgiftes.OnderdelenId);
return View(uitgiftes);
}
The Controller where the info comes from:
private readonly ApplicationDbContext _context;
public OnderdelensController(ApplicationDbContext context)
{
_context = context;
}
// GET: Onderdelens
public async Task<IActionResult> Index()
{
return View(await _context.Onderdelen.ToListAsync());
}
// GET: Onderdelens/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var onderdelen = await _context.Onderdelen
.FirstOrDefaultAsync(m => m.Id == id);
if (onderdelen == null)
{
return NotFound();
}
return View(onderdelen);
}
// GET: Onderdelens/Create
public IActionResult Create()
{
return View();
}
// POST: Onderdelens/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Naam,Omschrijving,VoorraadKg,PrijsPerKg")] Onderdelen onderdelen)
{
if (ModelState.IsValid)
{
_context.Add(onderdelen);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(onderdelen);
}
I have set everything to null for onderdelen(components) in the database.
Have I not made the relations between them good?
//Verbinding Many to Many Innameapparaat
modelBuilder.Entity<Innameapparaat>()
.HasKey(c => new { c.InnamensId, c.ApparaatId });
modelBuilder.Entity<Innameapparaat>()
.HasOne(pt => pt.Innames)
.WithMany(p => p.Innameapparaats)
.HasForeignKey(pt => pt.ApparaatId);
modelBuilder.Entity<Innameapparaat>()
.HasOne(pt => pt.Apparaten)
.WithMany(p => p.Innameapparaats)
.HasForeignKey(pt => pt.InnamensId);
//Verbinding Many to Many Onderdeelapparaat
modelBuilder.Entity<Onderdeelapparaat>()
.HasKey(c => new { c.OnderdelenId, c.ApparaatId });
modelBuilder.Entity<Onderdeelapparaat>()
.HasOne(pt => pt.Onderdelen)
.WithMany(p => p.Onderdeelapparaats)
.HasForeignKey(pt => pt.ApparaatId);
modelBuilder.Entity<Onderdeelapparaat>()
.HasOne(pt => pt.Apparaten)
.WithMany(p => p.Onderdeelapparaats)
.HasForeignKey(pt => pt.OnderdelenId);
}
public DbSet<RobinGroot.Models.Uitgiftes> Uitgiftes { get; set; }
public DbSet<RobinGroot.Models.Apparaten> Apparaten { get; set; }
public DbSet<RobinGroot.Models.Innames> Innames { get; set; }
public DbSet<RobinGroot.Models.Onderdelen> Onderdelen { get; set; }
I have searched for this problem but I could not solve the problem on my own so I could really use some help with this.
Upvotes: 1
Views: 48
Reputation: 43860
Fix your controller constructor:
private readonly ApplicationDbContext _context;
private UserManager<Medewerker> _userManager;
//class constructor
public UitgiftesController(
UserManager<Medewerker> userManager,
ApplicationDbContext context
)
{
_userManager = userManager;
_context=context;
}
and your select list:
ViewData["OnderdelenId"] = new SelectList(_context.Set<Onderdelen>().ToList(), "Id", "Naam");
Upvotes: 2