Hello, It's my first post on this forum, hoping for a positive response.
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)
<td> @Html.DisplayFor(modelItem => item.Manufacturers.Manufacturer) </td> <td> @Html.DisplayFor(modelItem => item.CA.CAName) </td> ListDinghies Action in Controller
public async Task<IActionResult> ListDinghies() { return View(await _context2.Dinghies.ToListAsync()); } AddDinghies Post Action in Controller
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) { if (ModelState.IsValid) { //_context2.Manufacturers.Include(e => e.Manufacturer).FirstOrDefault(m => m.id == ); _context2.Add(dg); await _context2.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(dg); } Dinghies.cs (Only showing section where foreign keys and navigation properties are added)
[ForeignKey("Manufacturers")] public int ManufacturerID { get; set; } public Manufacturers Manufacturers { get; set; } [ForeignKey("CA")] public int ClassAssociationID { get; set; } public CA CA { get; set; } Manufacturers.cs (for reference, if problem is there)
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Dinghysailing.info.Models { public class Manufacturers { [Key] public int id { get; set; } [Column(TypeName = "nvarchar(50)")] [Required(ErrorMessage = "This field is required")] [DisplayName("Manufacturer: ")] public string Manufacturer { get; set; } [Column(TypeName = "nvarchar(50)")] [Required(ErrorMessage = "This field is required")] [DisplayName("Type: ")] public string Type { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Website: ")] public string Website { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Email")] public string Email { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Facebook")] public string Facebook { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Address: ")] public string Address { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Postal Address")] public string PostalAddress { get; set; } [Column(TypeName = "nvarchar(10)")] [DisplayName("Postal No.")] public string PostalNo { get; set; } [Column(TypeName = "nvarchar(MAX)")] [DisplayName("Image Path")] public string ImagePath { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Country: ")] public string Country { get; set; } [Column(TypeName = "nvarchar(50)")] [DisplayName("Telephone: ")] public string Telephone { get; set; } } }
I have searched a lot on internet but couldn't find the solution, I believe only an expert can figure out the problem. Thanks