I have a mvc 3 vb.net app that I need to generate several viewbags for… I tried the following code to just drop a variable at the end of the viewbag name after each loop but it wont take it.. It says “Object variable or With block variable not set.” when it his the viewbag.status(p) line on the second pass it makes through the loop…
Dim p As Integer = 0
For Each registrant In b
Dim _regi As attendance = registrant
Dim _status As New List(Of String)
If Not String.IsNullOrWhiteSpace(_regi.Completed_Class) Then
_status.Add(_regi.Completed_Class)
End If
_status.Add("--")
_status.Add("Absent")
_status.Add("Left Early")
_status.Add("Completed")
ViewBag._status(p) = _status
p = p + 1
Next
And the view looks like this:
@ModelTYPE List(Of xxxxxx.attendance)
@Code
ViewData("Title") = "Class Attendance Record"
End Code
@Using Html.BeginForm
@<fieldset>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Registrant ID</th>
<th>Course Status</th>
<th>Comments</th>
</tr>
@For r As Integer = 0 To Model.Count - 1
Dim i As Integer = r
@Html.HiddenFor(Function(m) m(i).id)
@Html.HiddenFor(Function(m) m(i).course_ref)
@<tr>
<td>@Html.DisplayFor(Function(m) m(i).firstName)
@Html.HiddenFor(Function(m) m(i).firstName)
</td>
<td>@Html.DisplayFor(Function(m) m(i).lastName)
@Html.HiddenFor(Function(m) m(i).lastName)
</td>
<td>@Html.DisplayFor(Function(m) m(i).reg_id)
@Html.HiddenFor(Function(m) m(i).reg_id)
</td>
<td>@Html.DropDownListFor(Function(m) m(i).Completed_Class, New SelectList(ViewBag._status(i)))
@Html.HiddenFor(Function(m) m(i).Completed_Class)
</td>
<td>@Html.TextBoxFor(Function(m) m(i).Comments, New With {.class = "AttenComment"})
@Html.HiddenFor(Function(m) m(i).Comments)
</td>
</tr>
Next
</table>
<p><input type="submit" name="submit" /></p>
</fieldset>
End Using
Any ideas???
I simply created the viewbag inside the for each loop of my view… So that way everytime it went through the loop to handle each item in the table it would generated that items viewbag for the dropdown as well..