不会让我在cshtml页面上调出我的列表



给定

另一个cshtml中的cshtml部分

@model List<Participant>;
@{
if(Model != null)
{
foreach(Participant i in Model)
{
<p>@i.ParticipantFirstName</p>
}
}
if(Model == null)
{
<p>placeholder:: the list is empty</p>
}
}
<p>test</p>

以及控制器

[HttpGet]
[Route("dashboard")]
public IActionResult Dashboard()
{
if(HttpContext.Session.GetInt32("UserId") == null)
{
return RedirectToAction("Index","Home");
}
ViewBag.loggedinUser = HttpContext.Session.GetInt32("UserId");
HttpContext.Session.SetInt32("DashboardTab", 0);
List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();
return View("Dashboard", allParticipants);
}

这个模型,

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace LeagueProject.Models
{
public class ViewModel
{
private Context db;
public ViewModel(Context context)
{
db = context;
}
public Participant participant { get; set; }
public class Participant
{
[Key]
public int ParticipantId { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantFirstName { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantLastName { get; set; }
[Required(ErrorMessage = "is required")]
public string ParticipantGender { get; set; }
// [Required(ErrorMessage = "is required")]
// [Range(8, 20, ErrorMessage="this league if roages 8-19")]
// public int ParticipantAge { get; set; }
[Required(ErrorMessage="Need a date of birth")]
[DataType(DataType.Date)]
public System.DateTime ParticipantDOB { get; set; }
public int UserId { get; set; }
public User Parent { get; set; }
public List<MMLeagueParticipant> allLeagues { get; set; }
}
}

}

我得到了异常

InvalidOperationException:传递到ViewDataDictionary的模型项的类型为"System"。集合。通用的列出"1[LeagueProject.Models.Participant]",但此ViewDataDictionary实例需要类型为"LeagueProject"的模型项。模型。ViewModel"。

我不明白为什么会出现错误。我是这个框架的新手。这在之前的一个类似项目中起到了作用。

ViewModel:中应为List<Participant>

public class ViewModel
{
//...
public List<Participant> participants { get; set; }
}

然后在控制器中,将此ViewModel返回到主视图。

[HttpGet]
[Route("dashboard")]
public IActionResult Dashboard()
{
if(HttpContext.Session.GetInt32("UserId") == null)
{
return RedirectToAction("Index","Home");
}
ViewBag.loggedinUser = HttpContext.Session.GetInt32("UserId");
HttpContext.Session.SetInt32("DashboardTab", 0);
List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();
var viewmodel = new ViewModel();
viewmodel.participants = allParticipants;
return View("Dashboard", viewmodel);
}