How to create a create function using data from tables with n-to-n ratio?

Asked

Viewed 38 times

0

It turns out that the create function that the other intern did doesn’t work because it has two tables with relation of n-n and in the function it just goes to fetch data and records them in one. I just can’t find a way to add another table or use the relational table.

This is the code of the create function 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 for the code_def and description_def on the table def_defect but I need you to go get the cod_process table Pro_Process.

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

1 answer

0


I figured out how to solve.

After all, MVC is really a great helper tool, but for those like me, 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 was 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

Might help someone who has the same problem I had

Browser other questions tagged

You are not signed in. Login or sign up in order to post.