C# 不允许我在cshtml页面上拉列表

C# 不允许我在cshtml页面上拉列表,c#,asp.net,asp.net-mvc,asp.net-core,C#,Asp.net,Asp.net Mvc,Asp.net Core,给予 cshtmlpartial在另一个cshtml @model List<Participant>; @{ if(Model != null) { foreach(Participant i in Model) { <p>@i.ParticipantFirstName</p> } } if(Model == null) {

给予

cshtml
partial在另一个
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>
@型号列表;
@{
如果(型号!=null)
{
foreach(模型中的参与者i)
{
@i、 参与者姓名

} } if(Model==null) { 占位符::列表为空

} } 试验

和一个控制器

    [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);
    }
[HttpGet]
[路线(“仪表板”)]
公共IActionResult仪表板()
{
if(HttpContext.Session.GetInt32(“UserId”)==null)
{
返回重定向到操作(“索引”、“主页”);
}
ViewBag.loggedinUser=HttpContext.Session.GetInt32(“用户ID”);
HttpContext.Session.SetInt32(“仪表板选项卡”,0);
列出allParticipants=db.Participants.Include(i=>i.Parent.ToList();
返回视图(“仪表板”,所有参与者);
}
而这个模型,

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; }
    }
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用Microsoft.EntityFrameworkCore;
名称空间LeagueProject.Models
{
公共类视图模型
{
私有上下文数据库;
公共视图模型(上下文)
{
db=上下文;
}
公共参与者{get;set;}
公开课参与者
{
[关键]
公共int ParticipantId{get;set;}
[必需(ErrorMessage=“is Required”)]
[MinLength(2,ErrorMessage=“必须至少包含2个字符”)]
公共字符串ParticipantFirstName{get;set;}
[必需(ErrorMessage=“is Required”)]
[MinLength(2,ErrorMessage=“必须至少包含2个字符”)]
公共字符串ParticipantLastName{get;set;}
[必需(ErrorMessage=“is Required”)]
公共字符串ParticipantGender{get;set;}
//[必需(ErrorMessage=“is Required”)]
//[范围(8,20,ErrorMessage=“如果roages 8-19,则此联盟”)]
//公共int参与{get;set;}
[必需(ErrorMessage=“需要出生日期”)]
[数据类型(DataType.Date)]
public System.DateTime ParticipantDOB{get;set;}
public int UserId{get;set;}
公共用户父项{get;set;}
公共列表所有联盟{get;set;}
}
}
}

我有个例外

InvalidOperationException:传递到ViewDataDictionary的模型项的类型为“System.Collections.Generic.List`1[LeagueProject.Models.Participant]”,但此ViewDataDictionary实例需要类型为“LeagueProject.Models.ViewModel”的模型项

我不明白为什么我会出错。我不熟悉这个框架。这在以前的一个类似项目中起到了作用。

它应该是ViewModel中的
List

public class ViewModel
{
    //...

    public List<Participant> participants { get; set; }

}
公共类视图模型
{
//...
公共列表参与者{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);
}
[HttpGet]
[路线(“仪表板”)]
公共IActionResult仪表板()
{
if(HttpContext.Session.GetInt32(“UserId”)==null)
{
返回重定向到操作(“索引”、“主页”);
}
ViewBag.loggedinUser=HttpContext.Session.GetInt32(“用户ID”);
HttpContext.Session.SetInt32(“仪表板选项卡”,0);
列出allParticipants=db.Participants.Include(i=>i.Parent.ToList();
var viewmodel=新的viewmodel();
viewmodel.participants=所有参与者;
返回视图(“仪表板”,视图模型);
}

我还尝试在我的主cshtml中添加此内容,在这里我调用包含分部的选项卡:::您能在另一个cahtml中显示包含分部的行吗