在每个循环中添加变量以查看包名称
问题描述:
我有一个mvc 3 vb.net应用程序,我需要为其生成几个viewbags ...我尝试了下面的代码,在viewbag名称末尾放置一个变量每个循环,但它不会把它..它说:“对象变量或块变量未设置。”当他在这使得通过循环第二遍viewbag.status(P)行... ...在每个循环中添加变量以查看包名称
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
和视图看起来是这样的:
@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
任何想法?
答
我只是创建内部的viewbag我的观点的每个环......所以这样每次它经历的循环来处理每一个项目表中,将产生的项目viewbag的下拉列表,以及..
答
根据您提供的代码,ViewBag._status
实际上持有List(Of List(Of String))
。换句话说,每个`ViewBag._status(p)实际上都持有一个List(Of String),而_status本身就是一个列表或数组。
我想什么才能解决你的问题,先做所有的工作,然后在循环结束后设置ViewBag._status
。
Dim statusLists As New List(Of List(Of String))
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")
statusLists.Add(_status)
Next
ViewBag._status = statusLists
请参阅我的编辑上面我已经包括视图,所以你有一个想法,为什么这些需要是独特的。 – Skindeep2366
好吧,那部分工作...问题是我如何让那些从视图袋索引号在 – Skindeep2366
的下拉列表中我真的想到了它......我正在考虑努力进入它..我的观点已经是工作的一部分 – Skindeep2366