-
Nov 7th, 2017, 05:56 AM
#1
Thread Starter
New Member
Multi select in vb.net
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, i'm using vb.
Code:
' 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
-
Nov 7th, 2017, 08:51 AM
#2
Thread Starter
New Member
VB.NET Using Chosen Plugin
Does anyone know if I can use the plugin chosen for multiselect dropdown list in vb.net?
All examples of how to use this plugin are for #c language.
Thanks
-
Nov 7th, 2017, 09:31 AM
#3
Re: VB.NET Using Chosen Plugin
C# and VB.Net are essentially the same language, so there are several online converters to convert code between the two.
Try one or more of those, as there is a very good chance they will give you working VB.Net code (or at least code that only needs minor changes).
If you can't get it working yourself, show us the C# code and the VB.Net code, and tell us what the problem(s) are with the VB code.
-
Nov 7th, 2017, 09:52 AM
#4
Thread Starter
New Member
Re: VB.NET Using Chosen Plugin
I'm getting started in programming, so there's a lot I do not know.
So I have a process table and one of defects. I have a dropdown list working but only for 1 defect 1 process.
I need to change the code to allow associating a defect with multiple processes.
When looking for help I found the chosen plugin but all I can find out how to use it is in #c, so I asked whether or not to use it in vb.
In controller:
Code:
' 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
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
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
In view:
Code:
@Html.AntiForgeryToken()
@<div class="form-horizontal">
<h4>Create Defect</h4>
<hr />
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="form-group">
Process Code
@Html.DropDownList("ProcessesList", CType(ViewBag.ProcessCode, IEnumerable(Of SelectListItem)))
@Html.ValidationMessageFor(Function(model) model.selectedProcesses, "", New With {.class = "text-danger"})
</div>
<br />
<div class="form-group">
Code
<div>
@Html.EditorFor(Function(model) model.CODE_DEF, New With {.htmlAttributes = New With {.class = "form-control"}})
@Html.ValidationMessageFor(Function(model) model.CODE_DEF, "", New With {.class = "text-danger"})
</div>
</div>
What can I do?
Suggest something?
-
Nov 7th, 2017, 10:23 AM
#5
Re: VB.NET Using Chosen Plugin
I was going to say that the only chosen plugin I know of is JavaScript, rather than .NET, and that seems a bit more right than I thought. Multiselect in a WinForms app is relatively simple, but this appears to be a question about web stuff. Therefore, I've moved the thread to the ASP.NET forum, where you are more likely to get better responses.
My usual boring signature: Nothing
-
Nov 7th, 2017, 10:27 AM
#6
Re: Multi select in vb.net
This looks like ASP.NET, is that correct?
My usual boring signature: Nothing
-
Nov 7th, 2017, 10:49 AM
#7
Thread Starter
New Member
Re: Multi select in vb.net
-
Nov 7th, 2017, 12:58 PM
#8
Re: VB.NET Using Chosen Plugin
I need to change the code to allow associating a defect with multiple processes.
If your returning multiple processes from a defect, then save those processes in a List object. In your view, loop through the model containing those processes and then display them in tabular or list form. The table may have a button/link that will open another view for editing or other activity.
Code:
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ProcessName)
</th>
...
</tr>
@foreach (var item in Model.Processes) //Processes is a list/collection
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProcessName)
</td>
...
</tr>
}
</table>
- kgc
-
Nov 8th, 2017, 04:03 AM
#9
Thread Starter
New Member
Re: Multi select in vb.net
I believe this thread is the same with this one VS 2015 VB.NET Using Chosen Plugin. I did reply to your thread on Using Chosen Plugin.
Regards,
kgc
It's not working with my code...
can you try with my own code? Please?
-
Nov 8th, 2017, 04:32 AM
#10
Re: Multi select in vb.net
The code i provided was just a logic on how to display data in tabular form. You have to make it work by using your model and properties associated with that.
-kgc
-
Nov 8th, 2017, 05:18 AM
#11
Thread Starter
New Member
Re: Multi select in vb.net
Originally Posted by KGComputers
The code i provided was just a logic on how to display data in tabular form. You have to make it work by using your model and properties associated with that.
-kgc
The problem is that i don't know how to do that
-
Nov 8th, 2017, 12:40 PM
#12
Re: Multi select in vb.net
Another thing is that if you need controls that has multiselect capabilities, you can use ListBoxFor() helper for that. See example here: MultiSelect Listbox. The code is in C# but the idea can be applied to VB.NET as well.
- kgc
-
Nov 10th, 2017, 05:38 AM
#13
Thread Starter
New Member
Re: Multi select in vb.net
Good morning,
I saw the example you sent me.
but when trying to use in my work I have a problem,
I think it's because it's in the def_defect controller and try to fetch data from the pro_process table.
Because the dropdown must have the CODE_PROCESS of the PRO_PROCESS table.
I do not know what to do ...
My code stayed like this
Code:
' GET: DEF_DEFECT/Create
Function Create() As ActionResult
'PopProcessDropDownList()
Dim LCODE_PRO As PRO_PROCESS = New PRO_PROCESS
LCODE_PRO.LCODE_PROs = PopulatePROCESSES()
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 LCODE_PRO As PRO_PROCESS) As ActionResult
LCODE_PRO.LCODE_PROs = PopulatePROCESSES()
If (Not (LCODE_PRO.CODE_PRO) Is Nothing) Then
Dim SELECTEDITEMS As List(Of SelectListItem) = LCODE_PRO.LCODE_PROs.Where(Function(p) LCODE_PRO.CODE_PRO.Contains(String.Format(p.Text))).ToList()
ViewBag.message = "Selected Processes:"
For Each selecteditem In SELECTEDITEMS
selecteditem.Selected = True
ViewBag.message += "\n" + selecteditem.Text
Next
End If
Return View(Index)
'If ModelState.IsValid Then
' db.PRO_PROCESS.Add(PRO_PROCESS)
' db.SaveChanges()
' Return RedirectToAction("Index")
'End If
'Return View(pRO_PROCESS)
End Function
Private Shared Function PopulatePROCESSES() As List(Of SelectListItem)
Dim items As New List(Of SelectListItem)()
Dim constr As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
Using con As New SqlConnection(constr)
Dim query As String = "Select CODE_PRO FROM PRO_PROCESS"
Using cmd As New SqlCommand(query)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
items.Add(New SelectListItem())
End While
End Using
con.Close()
End Using
End Using
Return items
'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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|