I still have the same questions, how do I add a drop down box for Contacts.FullName on the Create and Edit views for the DevicesContoller? I am able to add the drop downs and save the related data for DeviceCategories and DeviceLocations but not Contacts.
Follow the many-to-many pattern in my previous links. Following the pattern make the LINQ and loading related data a lot easier.
More importantly, you have a general design issue because you are using Entity Framework entities, like Devices, as view models (the models used to populate views and post data). This type of approach only works for the most simplest of MVC applications. The model used in Views should be separate from the EB entities. Again, the tutorials in this site cover this concept.
Unfortunately, I was unable to commit my updates to your repo. Probably related to GitHub security.
public class Devices
{
[Key]
public int DeviceId { get; set; }
public int DeviceCategoryId { get; set; }
public int? DeviceLocationId { get; set; }
public string Description { get; set; }
public string SerialNumber { get; set; }
public virtual DeviceCategories DeviceCategory { get; set; }
public virtual DeviceLocations DeviceLocation { get; set; }
public int? ContactsId { get; set; }
public ICollection<Contacts> Contacts { get; set; }
public List<DeviceAssignments> DeviceAssignments { get; set; }
}
public class Contacts
{
[Key]
public int ContactId { get; set; }
public string FullName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Devices> Devices { get; set; }
public List<DeviceAssignments> DeviceAssignments { get; set; }
}
public class DeviceAssignments
{
[Key]
public int DeviceAssignmentId { get; set; }
public int DeviceId { get; set; }
public Devices Devices { get; set; }
public int ContactId { get; set; }
public Contacts Contacts { get; set; }
}
public class AssetsContext : DbContext
{
public AssetsContext(DbContextOptions<AssetsContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Devices>()
.HasMany(p => p.Contacts)
.WithMany(p => p.Devices)
.UsingEntity<DeviceAssignments>(
j => j
.HasOne(da => da.Contacts)
.WithMany(c => c.DeviceAssignments)
.HasForeignKey(pt => pt.ContactId),
j => j
.HasOne(da => da.Devices)
.WithMany(d => d.DeviceAssignments)
.HasForeignKey(da => da.DeviceId),
j =>
{
j.HasKey(t => new { t.DeviceId, t.ContactId });
});
}
public virtual DbSet<Contacts> Contacts { get; set; }
public virtual DbSet<DeviceAssignments> DeviceAssignments { get; set; }
public virtual DbSet<DeviceCategories> DeviceCategories { get; set; }
public virtual DbSet<DeviceLocations> DeviceLocations { get; set; }
public virtual DbSet<Devices> Devices { get; set; }
}
// GET: Devices/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Devices == null)
{
return NotFound();
}
var devices = await _context.Devices.Include(c => c.Contacts).FirstOrDefaultAsync(d => d.DeviceId == id);
ViewData["ContactsOptions"] = new SelectList(devices.Contacts, "ContactId", "FullName");
//var devices = await _context.Devices.FindAsync(id);
if (devices == null)
{
return NotFound();
}
ViewData["DeviceCategoryId"] = new SelectList(_context.DeviceCategories, "DeviceCategoryId", "DeviceCategoryName", devices.DeviceCategoryId);
ViewData["DeviceLocationId"] = new SelectList(_context.DeviceLocations, "DeviceLocationId", "DeviceLocationName", devices.DeviceLocationId);
return View(devices);
}