dynamic dropdownlist in ASP.net Razor MVC5 from a Many to many database

315 views Asked by At

Here is the look of my tables in MSSQL MS : MSSQL Management Studio As i’ve learned, Entity Framework is changing the schema to hide the middle database and made it look like this : VB view with Entity Framework

I dont really know how to use the middle table (and if i should use it or not).

The idea of my website is to be able to create a student (table etudiant) with multiples cursus (table course).

When i create a student (etudiant), I should be able to pick up for example 5 cursus (cours). By adding a dropdown list and a + button to make another dropdownlist appears.

When i look at the detail of a student, i should see all the curses he has choosen.

Thanks for you help !

1

There are 1 answers

1
Narcil On

Yes you need to use a "middle table" (called etuCours in your diagram). This is the correct way of handling a many to many relation.

You probably don't need mulitple dynamic dropdowns to select multiple course, i would implement a multi select "drop down" which is more of a list.

Have a look at http://www.w3schools.com/tags/att_select_multiple.asp

There also many options available using jQuery if you want it to look pretty. Like http://loudev.com/

Using Razor it would be:

@{
    var foo = new List<SelectListItem> { 
        new SelectListItem { Text = "Foo 1", Value = "1", Selected = true },
        new SelectListItem { Text = "Foo 2", Value = "2" },
        new SelectListItem { Text = "Foo 3", Value = "3", Selected = true }
    };
}

@Html.ListBox("foo", foo)