This is a mvc3 Razor project in vb.net. I have a need to create a dynamic table that 1) can contain 1 column to 3 columns and 1 to many rows... The items in the table are checkboxes.. I already have working helper methods in the below class however the method below is jacked up on the return since it just returns the string along with the actual string of the Ntd function, this is there simply for an idea of what should be happening at that point... I am lost as to generate these checkboxes in away that can be binded so the controller post method will save... If I just dump all the checkboxes on page they will all save and update correctly. The layout is just an eye sore..
This is the current view
@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
This is the helper method
<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
To call the helper method I just plan to replace the for each loop and its contents with
@Html.CreateCheckBoxTable(Model.Bodies)
This method is generating the appropriate table with the correct rows and columns but I am lost on the checkboxfor..
Any Ideas is greatly appreciated..
Below is the current output that is being generated..
<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>
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))
and it is sayingCheckBoxFor
is not a member of Html. – Skindeep2366 Mar 10 at 17:25