這是vb.net中的mvc3 Razor項目。我需要創建一個動態表,1)可以包含1列到3列和1到多行...表中的項目是複選框..我已經在下面的類中有工作助手方法,但下面的方法因爲它只是返回字符串以及Ntd函數的實際字符串,所以只是想知道在那個時候應該發生什麼......我迷失了在遠處生成這些複選框這可以綁定,所以控制器後的方法將保存...如果我只是轉儲頁面上的所有複選框,他們將全部保存並正確更新。佈局只是一個眼睛紅腫..Mvc3 HtmlHelper方法生成格式化的Checkbox表
這是當前視圖
@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
要調用的輔助方法,我只是打算更換每個循環及其內容與
@Html.CreateCheckBoxTable(Model.Bodies)
此方法正在生成適當的表與correc牛逼的行和列,但我失去了對checkboxfor ..
下面是正在產生的電流輸出..
<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>
當你從該行刪除語音標記,會發生什麼? Ntd(「@ Html.CheckBoxFor(Function(model)model.Bodies(i).certSelected)@ Html.DisplayFor(Function(f)f.Bodies(i).certName)@ Html.HiddenFor(Function(model)model。 (我).certBodyId)「) – Zeddy 2013-03-07 14:47:54
@ZafKhan我已經改變了這一行閱讀:'htmlDisplayer = htmlDisplayer + Ntd(Html.CheckBoxFor(Function(model)model.Bodies(i).certSelected)Html.DisplayFor(Function( f)body(i).certName)Html.HiddenFor(Function(model)model.Body(i).certBodyId))'它表示'CheckBoxFor'不是Html的成員。 – Skindeep2366 2013-03-10 17:25:11
@ Skindeep2366,試試我的答案,它會工作... – 2013-03-12 06:04:07