首页 > 解决方案 > 如何使用 Visual Basic.NET 和 ASP.NET MVC 创建下拉列表并检索所选项目的值?

问题描述

我想使用 VB.NET HTML MVC 下拉列表创建并将其选定的项目检索到控制器中的另一个方法中。

下面是检索下拉列表值的方法的代码:

Function InputSetup() As ActionResult
        Dim inputStreamsDataSetup_Tbl = db.InputStreamsDataSetup_Tbl.Include(Function(i) i.Streams_Tbl).Include(Function(i) i.InputStreamsVariable_Tbl)
        ViewBag.SubFlowsheet = New SelectList(GetSubflow(), "SubFlowsheet", "SubFlowsheet")
        Return View(inputStreamsDataSetup_Tbl.ToList())
    End Function

这是显示下拉列表的 vbhtml 标记

@Html.DropDownList("SubFlowsheet", Nothing, "--Select SubFlowsheet--", New With {Key .onchange = "location.href=' @Url.Action('BindStreams', 'InputStreamsDataSetup_Tbl')'"})

我想BindStreams在所选更改上调用函数

Function BindStreams(ByVal Flowsheet As String) As JsonResult

        Using con As New SqlConnection(Str)
            Using cmd As New SqlCommand("select * from Streams_Tbl where SubFlowsheet='" + Flowsheet + "'", con)
                Dim Streams As New List(Of Streams_Tbl)
                cmd.CommandType = CommandType.Text
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        Streams.Add(New Streams_Tbl With {
                                      .Stream_Name = sdr("Stream_Name").ToString(),
                                      .Stream_ID = sdr("Stream_ID").ToString()
                                    })
                    End While
                End Using
                con.Close()
                Return Json(Streams, JsonRequestBehavior.AllowGet)
            End Using
        End Using

    End Function

提前致谢

标签: htmlasp.net-mvcvb.net

解决方案


推荐阅读