How to create a create function using table data with relation from n to n?

0

It turns out that the create function that the other trainee did not work because it has two tables with n-n relation and in the function it is only going to fetch data and register them in one. But I can not figure out how to add another table or use the relational table.

This is the code of the create function that he left

' GET: DEF_DEFECT/Create
    Function Create() As ActionResult
        Return View()
    End Function

    ' POST: DEF_DEFECT/Create
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function create(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
        If ModelState.IsValid Then
            db.DEF_DEFECT.Add(dEF_DEFECT)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(dEF_DEFECT)
    End Function

He sent me to get code_def and description_def on table def_defect but I need to get cod_process from table Pro_Process .

These 2 tables, when normalized, gave rise to the table Process_Defect Can someone help me?

    
asked by anonymous 20.10.2017 / 15:30

1 answer

0

I found out how to solve it.

After all, MVC is really a great help tool, but for those of you starting out, it turns out to be a bit confusing. So I created a private sub to update the database and solved problem. :)

Private Sub UpdateDefectProcess(updateDefect As DEF_DEFECT, processSelected As String)
        'Dim selectedProcessHS = New HashSet(Of String)(processSelected)
        Dim selectedProcessHS = processSelected

        Dim defectProcess As IEnumerable(Of String) = New HashSet(Of String)(updateDefect.PRO_PROCESS.Select(Function(c) c.CODE_PRO))
        For Each c In db.PRO_PROCESS
            If selectedProcessHS.Contains(c.CODE_PRO.ToString()) Then
                updateDefect.PRO_PROCESS.Add(c)
            Else
                If defectProcess.Contains(c.CODE_PRO) Then
                    updateDefect.PRO_PROCESS.Remove(c)
                End If
            End If

        Next

    End Sub

And the create function looks like this,

 ' GET: DEF_DEFECT/Create
    Function Create() As ActionResult
        PopProcessDropDownList()
        Return View()
    End Function

    ' POST: DEF_DEFECT/Create
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
        If ModelState.IsValid Then
            db.DEF_DEFECT.Add(dEF_DEFECT)

            Dim processSelected As String = Request.Form("ProcessesList").ToString()

            UpdateDefectProcess(dEF_DEFECT, processSelected)

            db.SaveChanges()
            Return RedirectToAction("Index")

        End If
        Return View(dEF_DEFECT)
    End Function

It might help someone with the same problem I had

    
26.10.2017 / 16:29