これは、vb.net の mvc3 Razor プロジェクトです。1) 1 列から 3 列、1 列から多数の行を含むことができる動的テーブルを作成する必要があります。 Ntd関数の実際の文字列と一緒に文字列を返すだけなので、戻り時にジャッキアップされます。これは、その時点で何が起こっているべきかを理解するためのものです...これらのチェックボックスを離れて生成する方法がわかりませんバインドできるので、コントローラーのポストメソッドが保存されます...ページ上のすべてのチェックボックスをダンプすると、すべてが正しく保存および更新されます。レイアウトは目が痛いだけです..
これが現在のビューです
@ModelType xxxxxx.CourseModel
@Code
ViewData("Title") = "Edit Courses"
End Code
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Using Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With {.enctype = "multipart/form-data"})
@Html.ValidationSummary(True)
@<fieldset>
<legend>Edit Courses</legend>
@Html.HiddenFor(Function(model) model.cId)
<table style="float: left">
<tr>
<th>Certification Bodies</th>
</tr>
<tr>
@For _i As Integer = 0 To Model.Bodies.Count - 1
Dim i = _i
@<td>
@Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)
@Html.DisplayFor(Function(f) f.Bodies(i).certName)
@Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)
</td>
Next
</tr>
<tr>
<th><input type="submit" value="Save" /></th>
</tr>
</table>
</fieldset>
end using
これはヘルパーメソッドです
<Extension()> _
Public Function CreateCheckBoxTable(ByVal helper As HtmlHelper, ByVal d As List(Of CertBodyVM)) As MvcHtmlString
Dim htmlDisplayer As String = Table()
Dim counter As Integer = 0
For Each item In d
If counter = 0 Then
htmlDisplayer = htmlDisplayer + NRow()
End If
counter += 1
If Not counter >= 3 Then
htmlDisplayer = htmlDisplayer + Ntd("@Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies(i).certName)@Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)")
Else
counter = 0
htmlDisplayer = htmlDisplayer + CRow()
End If
Next
htmlDisplayer = htmlDisplayer + CTable()
Dim x As MvcHtmlString = MvcHtmlString.Create(htmlDisplayer)
Return x
End Function
Public Function Table() As String
Return String.Format("<table>")
End Function
Public Function CTable() As String
Return String.Format("</table>")
End Function
Public Function NRow() As String
Return String.Format("<tr>")
End Function
Public Function TdEnd() As String
Return String.Format("</td>")
End Function
Public Function CRow() As String
Return String.Format("</tr>")
End Function
Public Function Ntd(ByVal text As String) As String
Return String.Format("<td>{0}</td>", text)
End Function
ヘルパー メソッドを呼び出すには、for each ループとその内容を次のように置き換える予定です。
@Html.CreateCheckBoxTable(Model.Bodies)
このメソッドは、正しい行と列を持つ適切なテーブルを生成していますが、チェックボックスで迷っています..
以下は、生成されている現在の出力です。
<tr><td><table><tr><td>@Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies(i).certName)@Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)</td></table></td></tr>