Click here to Skip to main content
16,008,954 members
Articles / Programming Languages / C#

Configure Many-To-Many Relationship and ListBox Control in MVC and Entity Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Apr 2015CPOL2 min read 17.1K   5   4
Configure Many-To-Many Relationship and ListBox control in MVC and Entity Framework

Relationship Scenario (Many to Many Relationship)

Video: https://www.youtube.com/watch?v=nzmk1hrYYtQ

Relationship scenario (Many to Many Relationship)

  • Trainer can train on multiple Courses
  • E.g.- T1 Trainer trains on C1 and C2 Courses
  • Course can be handled by multiple Trainers
  • E.g.- 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 manager 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer HP
India India
I am working in HP as a dot net developer.Working in .net technologies(MVC,Entityframework,LINQ,Jquey

Comments and Discussions

 
QuestionDisplay all item from all table Pin
Member 142070474-Apr-19 3:07
Member 142070474-Apr-19 3:07 
Hello,

thank you for this article, I have a problem when I display items from all tables

// GET: PhoneCalls
      public ActionResult Index()
      {

          var phoneCalls = (from pa in db.ActionsPhone
                            join pcpa in db.PhoneCallActionPhones
                            on pa.IdPhoneAction equals pcpa.IdPhoneAction
                            join pc in db.PhoneCalls
                            on pcpa.IdPhoneCall equals pc.IdPhoneCall
                            join pcpm in db.MotifPhonePhoneCalls
                            on pc.IdPhoneCall equals pcpm.IdPhoneCall
                            join pm in db.MotifsPhone
                            on pcpm.IdPhoneMotif equals pm.IdPhoneMotif
                            select new
                            {
                                Id = pc.IdPhoneCall,
                                Sujet = pc.Subject,
                                Interlocuteu = pc.Interlocuteur,
                                Compan = pc.Company,
                                TypeCall = pc.CallTypes,
                                PhoneNum = pc.Phone,
                                ActionPhone = pa.NameAction,
                                MotifPhone = pm.NameMotif,
                                Date_and_tim = pc.Date_and_time,
                                statu = pc.Statuts

                            });



          return View(phoneCalls.ToList());
      }

QuestionNice job....but Pin
Murph13291-Feb-17 4:49
Murph13291-Feb-17 4:49 
AnswerRe: Nice job....but Pin
Murph13291-Feb-17 6:33
Murph13291-Feb-17 6:33 
QuestionNeed source code download link Pin
Hanumantha reddy GS14-Jun-15 23:48
Hanumantha reddy GS14-Jun-15 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.