场景:我们的 MVC View 中有一个列表框,用户可以突出显示多个值。我希望能够将这些选定的值(如果可能的话?)保存为我们的 '08 SQL 数据库中的逗号分隔单元格值。
下图显示了我试图解释的内容。 注意列表框中的选择提前致谢!
这是我们保存从 Positionnumber DDL(带有多个突出显示的列表框)传入的值的地方。
> <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
最佳答案
您需要确保列表框的名称字段与 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 )
关于vb.net - 从列表框中选择值并将其保存到 SQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10344382/