none
DropDownList with multiple selections RRS feed

  • Question

  • I have a table defects and one of processes, and a defect can have one or several processes. For now I have to work a dropdownlist for one defect, one process, what changes I need to make to be able to select several processes, follow the code below

    ' 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
    
            ' GET: DEF_DEFECT/Edit/5
            Function Edit(ByVal id As String) As ActionResult
                If IsNothing(id) Then
                    Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
                End If
    
                Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)
                If IsNothing(dEF_DEFECT) Then
                    Return HttpNotFound()
                End If
    
                PopulateProcessesDropDownList(dEF_DEFECT.PRO_PROCESS)
    
                Return View(dEF_DEFECT)
            End Function
    
            Sub PopProcessDropDownList()
                Dim listSelectListItems As List(Of SelectListItem) = New List(Of SelectListItem)
    
                For Each p As PRO_PROCESS In db.PRO_PROCESS
                    Dim selectItem As SelectListItem = New SelectListItem()
                    selectItem.Text = p.CODE_PRO
                    listSelectListItems.Add(selectItem)
                Next
    
                ViewBag.ProcessCode = listSelectListItems
            End Sub
    
    
            Sub PopulateProcessesDropDownList(selectedprocesses As Object)
                ' Dropdown Lists
                Dim allProcesses = From s In db.PRO_PROCESS
                      Order By s.CODE_PRO
                      Select s.CODE_PRO, s.DESCRIPTION_PRO Distinct
    
                ViewBag.PList = New SelectList(allProcesses, "CODE_PRO", "CODE_PRO", selectedValue:=selectedprocesses)
            End Sub
    
            ' POST: DEF_DEFECT/Edit/5
            '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 Edit(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
                If ModelState.IsValid Then
                    Dim processToUpdate = db.DEF_DEFECT _
                                   .Include(Function(i) i.PRO_PROCESS) _
                                   .Where(Function(i) i.CODE_DEF = dEF_DEFECT.CODE_DEF) _
                                   .Single()
    
                    Dim processSelected As String = Request.Form("ProcessesList").ToString()
    
                    UpdateDefectProcess(processToUpdate, processSelected)
    
                    db.Entry(processToUpdate).State = EntityState.Modified
    
                    db.SaveChanges()
                    Return RedirectToAction("Index")
                End If
                Return View(dEF_DEFECT)
            End Function
    
            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

    Wednesday, October 25, 2017 3:48 PM

Answers

  • Hi somebody_pt,

    Since this forum is discussing and asking questions about the Visual Basic programming language, IDE, libraries, samples, and tools .  And your issue is more related to Web Application. please go to the asp.net forum to get the professional help.

    https://forums.asp.net/

    Thank you for participating in the forum activities.

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by somebody_pt Friday, October 27, 2017 7:14 AM
    Friday, October 27, 2017 1:20 AM
    Moderator

All replies

  • Hi somebody_pt,

    According to your description, you use DropDownList control in your application, can you tell me that your application is Web application or not?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 26, 2017 8:39 AM
    Moderator
  • Yes, it is.

    can you help me?

    Thursday, October 26, 2017 9:39 AM
  • Hi somebody_pt,

    Since this forum is discussing and asking questions about the Visual Basic programming language, IDE, libraries, samples, and tools .  And your issue is more related to Web Application. please go to the asp.net forum to get the professional help.

    https://forums.asp.net/

    Thank you for participating in the forum activities.

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by somebody_pt Friday, October 27, 2017 7:14 AM
    Friday, October 27, 2017 1:20 AM
    Moderator