我在控制器中有这个:
public ActionResult Create(int id)
{
var enrollment = db.Events.FirstOrDefault(e => e.id == id);
(...)
var query = (from g in db.UserClass
join u in db.UserProfiles on g.UserId equals u.UserId
where g.ClassId == enrollment.ClassId
select new
{
UserClassId = g.UserClassId,
NameUser = u.Name
}).ToList();
ViewBag.UserClassId = new SelectList(query, "UserClassId", "NameUser");
return View("Create", attendance);
}
在视图中:
@model List<Project.Models.Attendance>
(...)
int y = 0;
foreach (var x in Model)
{
<tr>
<td>@Html.DropDownListFor(a => a[y].UserClassId, (SelectList)ViewBag.UserClassId)</td>
</tr>
y++;
}
</table>
(...)
当我点击提交时,会出现此错误:
没有类型为'IEnumerable'的ViewData项 键有'[0] .UserClassId'。
我已经尝试过在其他问题中找到的一些方法,例如将ViewBag转换为ViewData和<td>@Html.DropDownListFor(a => a[j].UserClassId, (IEnumerable<SelectListItem>)ViewBag.UserClassId)</td>
,但没有任何效果。我被困在这里,有人能帮助我吗?感谢