I'm trying to develop a ASP.net mvc application using entity framework 6.
There are 2 entities as Driver
& DriverType
. When adding a new driver to the database, User should be able to select the Driver Type from a DropDownList.
When submitting the form, The ID of the selected DriverType should be added to the DriverTypeID
(Foreign Key) column in the Driver
Table.
My Question is how to fetch all the DriverTypes to the DropDownList when loading the page & How to pass the ID of the selected DriverType to the Driver
table?
My Model classes as follows.
Driver Class
public partial class Driver
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Driver()
{
this.trip_tab = new HashSet<Trip>();
}
public string ID { get; set; }
public string DriverTypeID { get; set; }
public string VehicleID { get; set; }
public string Name { get; set; }
public string ContactNo { get; set; }
public string Comment { get; set; }
public virtual DriverType drivertype_tab { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Trip> trip_tab { get; set; }
}
Driver Type Class
public partial class DriverType
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DriverType()
{
this.driver_tab = new HashSet<Driver>();
}
public string ID { get; set; }
public string Type { get; set; }
public string Comment { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Driver> driver_tab { get; set; }
}
}
I'm using database first approach.