first of all im real beginner with c# and .net i started 10 days ago so im trying to display datas from a one-to-many relation but i have an error System.NullReferenceException : 'Object reference not set to an instance of an object.(i checked db there are datas in this table) and i dont understand why. I read many post about this but it never works, my code is almost the same than the example on the microsoft doc page but mine doesnt work.
model
[Display(Name = "Formations")]
public virtual ICollection<Courses> Course { get; set; }
}
public class Courses
{
[Key]
public int CourseId { get; set; }
[Display(Name = "Formations")]
public string Course { get; set; }
public virtual Users users { get; set; }
}
Controller
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var users = await _context.Users
.FirstOrDefaultAsync(m => m.Id == id);
if (users == null)
{
return NotFound();
}
return View(users);
View
@foreach (var item in Model.Course)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Course)
</td>
</tr>
}