选择并将值从列表框保存到SQL DB

时间:2012-04-27 03:30:50

标签: vb.net asp.net-mvc-3 sql-server-2008

场景:我们的MVC视图中有一个列表框,用户可以突出显示多个值。我希望能够在'08 SQL数据库中将这些选定的值(如果可能的话)保存为逗号分隔的单元格值。

下图显示了我试图解释的内容。 请注意LISTBOX中的选择提前致谢!

View of Listbox selections 这是我们从Positionnumber DDL(Listbox w / mulitple highlight)保存传入的值的地方。

> <HttpPost()>
>         Function Edit(wsmonitor As WSMonitor, ByVal vbpositionnumberDDL As Integer, ByVal PassedCounty As Integer, ByVal
> MonitorTypeDDL As String) As ActionResult
>             wsmonitor.PositionNumber = vbpositionnumberDDL
>             wsmonitor.MonitorType = MonitorTypeDDL
>             wsmonitor.county = PassedCounty
> 
>             If ModelState.IsValid Then
>                 db.Entry(wsmonitor).State = EntityState.Modified
>                 db.SaveChanges()
>                 Return RedirectToAction("Index")
>             End If
> 
>             Return View(wsmonitor)
>         End Function

1 个答案:

答案 0 :(得分:0)

您需要确保列表框的名称字段与Edit方法中的参数匹配。 然后,将参数vbpositionnumberDDL的Type更改为字符串数组。

然后,您将在列表框中的每个选中它的数组中传递。 然后很容易将其转换为逗号字符串。

我的VB.NET并不好,但我认为这样可行

Function Edit(wsmonitor As WSMonitor, ByVal vbpositionnumberDDL As **String()**, ByVal PassedCounty As Integer, ByVal
> MonitorTypeDDL As String) As ActionResult

您可以使用

加入字符串
Dim foo = [String].Join(",", vbpositionnumberDDL )