Najam Ul

Najam Ul

  • NA
  • 9
  • 488

Foreign Key properly set but can't access the data through foreign key

Aug 29 2020 6:32 AM

Hello, It's my first post on this forum, hoping for a positive response. 

I have three classes, Dinghy, Manufacturer and ClassAssociation, each Dinghy have 1 manufacturer and 1 class association and so each table of Dinghy includes its respective foreign keys for manufacturer and class association. The problem is that, when I add Dingies to database through a form, everything works well and table of Dinghies receives the IDs of Manufacturer and Class Association, but when I proceed to display the name of Manufacturer and Class Association using the foreign keys, it shows null. Debugging has shown me that the object of Manufacturer and Class Association is storing null. Why is it so when I have properly configured the foreign keys?

I am attaching the relevant code for you guys to dig out the issue.

.CSHTML File (I am using this syntax to display the data)

  1. <td>  
  2.               @Html.DisplayFor(modelItem => item.Manufacturers.Manufacturer)  
  3.           </td>  
  4.           <td>  
  5.               @Html.DisplayFor(modelItem => item.CA.CAName)  
  6.           </td>  
ListDinghies Action in Controller
  1. public async Task<IActionResult> ListDinghies()  
  2.         {  
  3.             return View(await _context2.Dinghies.ToListAsync());  
  4.         }  
AddDinghies Post Action in Controller
  1. public async Task<IActionResult> AddDinghies([Bind("id,Name,OlympicBoat,Type,TypeCrew,TypePurpose,Length,Beam,Draft,SailAreaUpwind,GennakerSailArea,SpinnakerSailArea,SAOptional,SAOptional2,Weight,PYS,ImagePath,CrewPersons,CrewWeight,Description,DesignBy,DesignYear,OlympicSectionViewSwitch,ManufacturerID,ClassAssociationID")] Dinghies dg)  
  2.         {  
  3.             if (ModelState.IsValid)  
  4.             {  
  5.                //_context2.Manufacturers.Include(e => e.Manufacturer).FirstOrDefault(m => m.id == );  
  6.                 _context2.Add(dg);  
  7.                 await _context2.SaveChangesAsync();  
  8.                 return RedirectToAction(nameof(Index));  
  9.             }  
  10.             return View(dg);  
  11.         }  
Dinghies.cs (Only showing section where foreign keys and navigation properties are added)
  1. [ForeignKey("Manufacturers")]  
  2.         public int ManufacturerID { getset; }  
  3.         public Manufacturers Manufacturers { getset; }  
  4.   
  5.         [ForeignKey("CA")]  
  6.         public int ClassAssociationID { getset; }  
  7.         public CA CA { getset; }  
Manufacturers.cs (for reference, if problem is there)
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using System.ComponentModel;  
  6. using System.ComponentModel.DataAnnotations;  
  7. using System.ComponentModel.DataAnnotations.Schema;  
  8.   
  9. namespace Dinghysailing.info.Models  
  10. {  
  11.     public class Manufacturers  
  12.     {  
  13.         [Key]  
  14.         public int id { getset; }  
  15.   
  16.         [Column(TypeName = "nvarchar(50)")]  
  17.         [Required(ErrorMessage = "This field is required")]  
  18.         [DisplayName("Manufacturer: ")]  
  19.         public string Manufacturer { getset; }  
  20.   
  21.         [Column(TypeName = "nvarchar(50)")]  
  22.         [Required(ErrorMessage = "This field is required")]  
  23.         [DisplayName("Type: ")]  
  24.         public string Type { getset; }  
  25.   
  26.         [Column(TypeName = "nvarchar(50)")]  
  27.         [DisplayName("Website: ")]  
  28.         public string Website { getset; }  
  29.   
  30.         [Column(TypeName = "nvarchar(50)")]  
  31.         [DisplayName("Email")]  
  32.         public string Email { getset; }  
  33.   
  34.         [Column(TypeName = "nvarchar(50)")]  
  35.         [DisplayName("Facebook")]  
  36.         public string Facebook { getset; }  
  37.   
  38.         [Column(TypeName = "nvarchar(50)")]  
  39.         [DisplayName("Address: ")]  
  40.         public string Address { getset; }  
  41.   
  42.         [Column(TypeName = "nvarchar(50)")]  
  43.         [DisplayName("Postal Address")]  
  44.         public string PostalAddress { getset; }  
  45.   
  46.         [Column(TypeName = "nvarchar(10)")]  
  47.         [DisplayName("Postal No.")]  
  48.         public string PostalNo { getset; }  
  49.   
  50.         [Column(TypeName = "nvarchar(MAX)")]  
  51.         [DisplayName("Image Path")]  
  52.         public string ImagePath { getset; }  
  53.   
  54.         [Column(TypeName = "nvarchar(50)")]  
  55.         [DisplayName("Country: ")]  
  56.         public string Country { getset; }  
  57.   
  58.         [Column(TypeName = "nvarchar(50)")]  
  59.         [DisplayName("Telephone: ")]  
  60.         public string Telephone { getset; }  
  61.     }  
  62. }  

I have searched a lot on internet but couldn't find the solution, I believe only an expert can figure out the problem. Thanks

Answers (1)