将 Dictionary<string, List<model>> 模型传递给视图

passing a Dictionary<string, List<model>> model to a view

不清楚如何将此模型传递到我的视图中。 我有一个列表模型被传递到我的视图中。但我想根据在 ViewBag 上登录的 User 来分隔列表中的 Participant

当前视图代码(有效): 此代码可以正常工作,但它不会根据登录的用户将参与者分开。它显示整个列表。我在我的 foreach 行中的 @modelLeague 和我的 secondforeach 中的 MMLeagueParticipants 下得到了红色的 squilies。但它仍然有效。

错误:

当我将鼠标悬停在 @modelLeague 上时: The type or namespace name 'League' could not be found (are you missing a using directive or an assembly reference?)[LeagueProect]

当我将鼠标悬停在 MMLeagueParticipant 上时: 找不到类型或命名空间名称 'MMLeagueParticpant'(是否缺少 using 指令或程序集引用?`


//RED SQUIGLY #1 @model
@model List<League>
    @{    
        if (Model != null && Model.Any())
        {
            //RED SQUIGLY #2 League
            foreach(League i in Model)
            {
                //RED SQUIGLY #3 MMLeagueParticipant
                foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
                {
                    <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
                }  
            }
        }
        else
        {
            <p>the list is empty</p>
        }
    }

尝试分离(无效) 仍在使用 @model List<League>。 当我更改我的代码以尝试将其分开时,它完全崩溃了

    <p>Your kid(s):</p>    
    if (Model != null && Model.Any())
    {
        foreach(League i in Model)
        {
            var userParticipants = i.allParticipants.Where(p => p?.child?.Parent?.UserId == ViewBag.UserId).ToList();
            foreach (MMLeagueParticipant mmLeagueParticipant in userParticipants)
            {
                <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
            }  
        }
    }
    <p>not your kid(s):</p>
    if (Model != null && Model.Any())
    {
        foreach(League i in Model)
        {
            var userParticipants = i.allParticipants.Where(p => p?.child?.Parent?.UserId != ViewBag.UserId).ToList();
            foreach (MMLeagueParticipant mmLeagueParticipant in userParticipants)
            {
                <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
            }  
        }
    }

我尝试这个的错误:

NullReferenceException: Object reference not set to an instance of an object.

不喜欢台词:

我假设我将模型传递到视图的方式不正确。但完全不确定为什么一种方法有效而另一种方法无效。

控制器详情:

当我 当前视图代码 (如上所示)显示所有 Participants 时,选项卡按需要工作,当我 没有根据参与者是否属于登录用户来区分参与者。

我的 RosterPageBasketball 中的每个选项卡都包含以下代码:@await Html.PartialAsync("_Displayeach", Model["bbM#and#"])

我认为问题出在哪里: ("_Displayeach", Model["bbM7and8"])

我发送到我的部分的模型是模型 ["bbM7and8"] 但我在我的 _Displayeach 中使用的模型是 @model List<League>。不确定如何或是否可以传入 @model Dictionary<string, List<League>>.


控制器:

public async Task<IActionResult> GetBasketballRoster()
{
    String[] leagueNames = new[]
    {
        "bbM7and8",
        "bbM9and10",
        "bbM11and12",
        "bbM13and14",
        "bbM15and16",
        "bbM17and18",
        "bbF7and8",
        "bbF9and10",
        "bbF11and12",
        "bbF13and14",
        "bbF15and16",
        "bbF17and18"
    };
        Dictionary<string, List<League>> d = new Dictionary<string, List<League>>();
        foreach (var name in leagueNames)
        {
            List<League> bbLeagues = await db.Leagues
            .Where(l => l.sport == "Basketball")
            .Where(l => l.ageRange==name)
            .ToListAsync();

            foreach (League league in bbLeagues)
            {
                List<MMLeagueParticipant> leagueParticipants = await db.Entry(league)
                    .Collection(l => l.allParticipants)
                    .Query() // <-- This is needed to allow for `Include()`
                    .Include(mmp => mmp.child)
                    .ToListAsync();
            }
            d.Add(name,bbLeagues);
        }
        return View("RosterPageBasketball", d);
}

从您的代码来看,您似乎想要显示 List<League>,它在特定 ageRange.

的部分视图中

这是一个您可以关注的工作演示:

型号:

public class League
{
    public string sport { get; set; }
    public string ageRange { get; set; }
    public List<MMLeagueParticipant> allParticipants { get; set; }
}
public class MMLeagueParticipant
{
    public child child { get; set; }
}
public class child
{
    public string ParticipantFirstName { get; set; }
    public string ParticipantLastName { get; set; }
}

RosterPageBasketball.cshtml:

@model Dictionary<string, List<League>>

@{ 
    var data = Model.Where(a => a.Key == "bbF17and18").Select(a => a.Value).FirstOrDefault();
}
@await Html.PartialAsync("_Displayeach", data)

_Displayeach.cshtml:

@model List<League>
@{
    if (Model != null && Model.Any())
    {
        foreach (League i in Model)
        {
            foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
            {
                <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
            }
        }
    }
    else
    {
        <p>the list is empty</p>
    }
}

控制器:

public async Task<IActionResult> Index()
{
    String[] leagueNames = new[]
    {
        "bbM7and8",
        "bbM9and10",
        "bbM11and12",
        "bbM13and14",
        "bbM15and16",
        "bbM17and18",
        "bbF7and8",
        "bbF9and10",
        "bbF11and12",
        "bbF13and14",
        "bbF15and16",
        "bbF17and18"
    };
    Dictionary<string, List<League>> d = new Dictionary<string, List<League>>();
    foreach (var name in leagueNames)
    {
        //hard coded the data....
        List<League> bbLeagues = new List<League>()
        {
            new League(){sport="Basketball",ageRange="bbF17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="San",ParticipantLastName="Da" } } } },
            new League(){sport="Basketball",ageRange="bbF17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Api",ParticipantLastName="Ee" } } }},
            new League(){sport="Basketball",ageRange="bbF9and10",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="May",ParticipantLastName="Fa" } } }},
            new League(){sport="Basketball",ageRange="bbM17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Ben",ParticipantLastName="He" } } }},
            new League(){sport="Basketball",ageRange="bbF15and16",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Jun",ParticipantLastName="Pa" } } }},
            new League(){sport="FootBall",ageRange="bbF15and16",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Pen",ParticipantLastName="Me" } } }}
        };

        var data = bbLeagues.Where(l => l.sport == "Basketball").Where(l => l.ageRange == name).ToList();
        d.Add(name, data);
    }
    return View("Index", d);
}