0

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.

JayNaz
  • 343
  • 1
  • 5
  • 24
  • The relevant code needs to be in the question. Not images of it. –  Jul 01 '16 at 11:28
  • Those are paste bin links. Not images. Anyway I'll add code to the question. – JayNaz Jul 01 '16 at 11:32
  • Start by using a view model that will contain a property `int SelectedDriver` and `IEnumerable DriverList` (and populate `DriverList` using `db.DriverTypes.Select(x => new SelectListItem(){ Value = x.ID, Text = x.Type });` Then refer the code in [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Jul 01 '16 at 11:47
  • you also need to add some controler and view code here – Sujit.Warrier Jul 01 '16 at 11:49

1 Answers1

0

you can create a selectList in your action and pass this selectList to a ViewBag and then access this ViewBag in your view. you should do this in your action first:

var allDriverTypes=_driverTypeService.GetAll();
ViewBag.DriverTypes=new SelectList(allDriverTypes,"ID","Title");

now in view you should do this:

@Html.DropDownListFor(model => model.DriverTypeID,(SelectList)ViewBag.DriverTypes)

after do this when your form submit and come back to post controller you can see driverTypeId that user choose bind to your model.

note: when you create a selectList you have a list that contain IDs and Titles that you want to show in your dropdown.