Configure Many-To-Many Relationship and ListBox control in MVC and Entty Frame Work
Relationship scenario (Many to Many Relationship)
Trainer can train on multiple Courses
Eg- T1 Trainer trains on C1 and C2 Courses
Course can be handled by multiple Trainers
Eg- C3 Course is handled by T3, T4 and T5trainers
Many to Many Relationship requires a new table(TrainsOnCourse) for storing the collections that are selected in the list box
Create a new Model Trainer
Execute the command "Add-Migration" in package manager console and update the database for adding the trainer table in the sql server database
Build the solution and create a new model for "TrainsOnCourse" for storing the collections selected in the list box
Add a DBSet entity property for the TrainsOnCourse in the DBContext class
Modify the Trainer model for adding navigational property and for saving the collection items selected in the listbox
Build the solution and execute the "Add-Migration" command in the package manager console for adding the new table "TrainsOnCourse"
Update the database for running the migration file in the sql server by executing the "update-database" command in package manger console
Open the sql server and new table "TrainsOnCourse" table should be created with 2 foreign keys
Add a new Controller "Trainer"
Select the "Tranier.cs" model and "DNSCContext"
Modify the Create Action method to add multiselect list values to be populated from the Course table from the database
Add a ListBoxControl in "Create.cshtml" view page in Trainer folder
Run the application and goto "Trainer/Create" Actionmethod by entering in the url.
We should be able to see the ListBox with set of values
Save the collection list items to the database
Modify the HTTPPost create action method to save the selected items
Modify the Edit Action method in the Trainer controller for handling Listbox control
Open the "Edit.cshtml" view page of trainer and add the ListBox control
Open the HttpPost Edit Action method of the Trainer controller and perform the required operations
Test the application with listbox values selected with Create and edit operations
CodeProject
Could you please share the source code with a download link. I've watched your Videos in youtube. They are very informative. Looking more articles and videos from you.
ReplyDeleteThanks