サンプル
■モデル
EmployeeModel.vb
Imports System.Collections.Generic
Namespace Models
Public Class EmployeeModel
Public Property IsChecked As Boolean
Public Property EmployeeId As String
Public Property EmployeeName As String
End Class
End Namespace
EmployeeListModel.vb
Imports System.Collections.Generic
Namespace Models
Public Class EmployeeListModel
Public Property Employees As List(Of EmployeeModel)
Public Sub New()
Me.Employees = New List(Of EmployeeModel)
End Sub
End Class
End Namespace
■コントローラ
SampleMvcController.vb
Imports System.Web.Mvc
Imports WebAppli.Models
Namespace Controllers
Public Class SampleMvcController
Inherits Controller
Function Index() As ActionResult
Dim model = New EmployeeListModel()
model.Employees = New List(Of EmployeeModel) From
{
New EmployeeModel() With {.IsChecked = False, .EmployeeId = "X001", .EmployeeName = "Mike"},
New EmployeeModel() With {.IsChecked = True, .EmployeeId = "X002", .EmployeeName = "Tom"},
New EmployeeModel() With {.IsChecked = True, .EmployeeId = "Y001", .EmployeeName = "Smith"},
New EmployeeModel() With {.IsChecked = False, .EmployeeId = "Z001", .EmployeeName = "Kevin"}
}
Return View(model)
End Function
Function SendResult(ByVal model As EmployeeListModel) As ActionResult
Return View(model)
End Function
End Class
End Namespace
■ビュー
Index.vbhtml
@ModelType WebAppli.Models.EmployeeListModel
@Code
ViewData("Title") = "Index"
Dim index As Integer = 0
End Code
@Using (Html.BeginForm("SendResult", "SampleMvc", FormMethod.Post))
@<table>
<thead>
<tr>
<th>Checked?</th>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@For Each employee In Model.Employees
@<tr>
<td>@Html.CheckBox(String.Format("Employees[{0}].IsChecked", index), employee.IsChecked)</td>
<td>@Html.TextBox(String.Format("Employees[{0}].EmployeeId", index), employee.EmployeeId)</td>
<td>@Html.TextBox(String.Format("Employees[{0}].EmployeeName", index), employee.EmployeeName)</td>
</tr>
index = index + 1
Next
</tbody>
</table>
@<input type="submit" value="Send" />
End Using
補足:以下のような書き方もできる
@For index = 0 To Model.Employees.Count - 1 Step 1
@<tr>
<td>@Html.CheckBoxFor(Function(model) model.Employees(index).IsChecked)</td>
<td>@Html.TextBoxFor(Function(model) model.Employees(index).EmployeeId)</td>
<td>@Html.TextBoxFor(Function(model) model.Employees(index).EmployeeName)</td>
</tr>
Next
SendResult.vbhtml
@ModelType WebAppli.Models.EmployeeListModel
@Code
ViewData("Title") = "SendResult"
End Code
<h2>SendResult</h2>
<table>
<thead>
<tr>
<th>Check?</th>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@For Each employee In Model.Employees
@<tr>
<td>@employee.IsChecked</td>
<td>@employee.EmployeeId</td>
<td>@employee.EmployeeName</td>
</tr>
Next
</tbody>
</table>