I have this in the controller:
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);
}
And in the view:
@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>
(...)
When i click submit, this error appears:
There is no ViewData item of type ‘IEnumerable’ that has the key '[0].UserClassId'.
I already tried some ways that i found in other questions, like turn the ViewBag into ViewData and <td>@Html.DropDownListFor(a => a[j].UserClassId, (IEnumerable<SelectListItem>)ViewBag.UserClassId)</td>
but nothing works. I'm stuck here, can someone help me? Thanks