我正在用数据库查询中的一些项目数据填充WebGrid。此WebGrid上的一列是复选框列。一旦单击“获取行”按钮,从而提交了表单,我便能够使用选中的复选框获取FormCollection。我的问题是如何获取与复选框关联的item_id和item_desc单元格?
控制器操作1:
Public Function ShowGridView_ListOfItems() As ActionResult
Try
Dim adapter As SqlDataAdapter
Dim ds As New DataSet
Dim con As SqlConnection
Dim con_string As String
con_string = "Data Source=localhost;Initial Catalog=[db];User Id=[ID];Password=[pass]"
con = New SqlConnection(con_string)
con.Open()
adapter = New SqlDataAdapter("select item_id, item_desc from inv_mast", con)
adapter.Fill(ds)
con.Close()
Dim objList As New List(Of Object)
objList = Rule.GetDatatableAsList(ds.Tables(0))
Return View("GridView_ListOfItems", objList)
Catch ex As Exception
Return View("Error", New HandleErrorInfo(ex, "GridView", "GridView"))
End Try
End Function
查看:
@ModelType System.Collections.Generic.IEnumerable(Of Object)
@Code
ViewData("Title") = "GridView_ListOfItems"
Layout = "~/Views/Shared/_VisualRuleLayout.vbhtml"
End Code
<link href="~/Content/bootstrap.css" rel="stylesheet" />
@Using Html.BeginForm("GetGridViewRows", "GridView", FormMethod.Post)
@<text>
<div Class="form-horizontal">
<form>
<h3>Items</h3>
@Code
Dim grid = New WebGrid(Model, canPage:=True, canSort:=True, rowsPerPage:=10)
grid.Pager(WebGridPagerModes.NextPrevious)
End Code
<div>
@Code
@grid.GetHtml(htmlAttributes:=New With {.id =
"grid", .class = "table table-bordered table-striped table-condensed", .name
= "grid1"},
emptyRowCellValue:="No Records Found",
headerStyle:="grid-header",
columns:=grid.Columns(
grid.Column(header:="Select", format:=Function() Html.CheckBox("chkboxname",
htmlAttributes:=New With {.id = "chkbox1", .value = "chkVal"})),
grid.Column("item_id", "Item ID"),
grid.Column("item_desc", "Description")))
End Code
</div>
<Button Class="btn btn-warning pull-right" type="submit">Get Rows<i class="glyphicon glyphicon-arrow-right"></i></Button>
</form>
</div>
</text>
End Using
控制器操作2:
Public Function GetGridViewRows(ByVal gridViewRows As FormCollection)
Dim checkedResults = gridViewRows.GetValues("chkboxname")
Dim results
If Not checkedResults Is Nothing Then
For Each value In checkedResults
If Not value = "false" Then
results += "(" & value & ")"
End If
Next
End If
ViewData("Message") = results
Return View("~/Views/MessageBox/MessageBoxOK.vbhtml")
End Function