Home > Mobile >  How to implement this ASP.NET MVC solution using C# that uses two models and a many-to-many table?
How to implement this ASP.NET MVC solution using C# that uses two models and a many-to-many table?

Time:01-07

I need to implement the following solution:

A user is in the drawing record form. The user clicks the "Assign Project" button in the drawing record form, which saves the drawing record as long as all required values are entered. The user is taken to a new form where the user can search for a project in the Projects table.

Next to each project record is an "Assign" button. When the user clicks the "Assign" button, the project is added/assigned to the drawing, which creates an entry in the ProjectDrawings many-to-many table. Two parameters need to be passed: the DrawingId and ProjectId of GUID type.

What are some ideas to get me started? Currently, the drawing record form is using the DrawingViewModel. The project record form is using the ProjectViewModel. I believe that for this solution, I would need to create a new model/viewmodel that combines attributes of drawing and project. For the search table, I want to use a DataTable.

CodePudding user response:

Make sure that relation between your entities is Many to Many first because it seems to me that logically one drawing can only be assigned to one project and one project can contain many drawings, which means that drawings table will have project table ID as foreign key. Kindly, correct me if I am wrong.

CodePudding user response:

I used the following URL resource to implement my solution: How to pass value from one action to another action having same controller

In addition to using TempData, the solution includes the following:

  1. In the Drawing Form, add an input type of Submit called "AssignProject".
  2. In the Drawing Controller, add "AssignProject" as a string parameter in the Create/Edit HttpPost actions. If "Assignproject" != null, redirect to action named "AssignProject" in the Drawing Controller and pass the DrawingId as the parameter.
  3. The "AssignProject" action displays a list view that uses the Project ViewModel. It uses DataTable to display and filter for project records. Each project record has a RenderLinkAsButton called "Assign" next to it.
  4. When an "Assign" button is clicked, the Url.Action is "AssignToDrawing" action in the Drawing Controller that passes ProjectId as a parameter.
  5. The "AssignToDrawing" action has the parameter of ProjectId and uses TempData to obtain the DrawingId to assign Drawing to Project.
  •  Tags:  
  • Related