首页 > 解决方案 > 根据登录的用户 ID 分隔列表

问题描述

我有League我传递给我的视图的列表。然后,视图应确定 的 id 是否与已登录Participant的 id 匹配;谁的 ID 保存在.UserIdUserViewBag

如果 id 匹配,我想在<p>Your kid(s):</p>;下显示列表 否则Participant' 将显示在<p>Everybody else's kid(s):</p>


_DisplayEach.cshtml

@model List<League>
@{
    <p>userId is: @ViewBag.UserId</p>
    <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>@mmLeagueParticipant.child.Parent.UserId</p>
            }  
        }
    }
    <p>Everybody elses kid(s):</p>
    if (Model != null && Model.Any())
    {

        foreach(League i in Model)
        {
            var allOtherUserParticipants = i.allParticipants.Where(p => p?.child?.Parent?.UserId != @ViewBag.UserId).ToList();
            foreach (MMLeagueParticipant mmLeagueParticipant in allOtherUserParticipants)
            {
                <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
                <p>@mmLeagueParticipant.child.Parent.UserId</p>
            }  
        }
    }
    else
    {
        <p>the list is empty</p>
    }
}

问题:

<p>Everybody elses kid(s):</p>无论 UserId 是否匹配,整个列表都显示在下方。而且没有任何东西<p>Your kid(s):</p>

错误: NullReferenceException: Object reference not set to an instance of an object. 然后我查看了我的控制器,看看它是如何返回为空的。

控制器详细信息:

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


仪表板控制器:

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();
            }
            var data = bbLeagues.Where(l => l.sport == "Basketball").Where(l => l.ageRange == name).ToList();
            d.Add(name, data);
        }
        }
        ViewBag.UserId = HttpContext.Session.GetInt32("UserId");
        Console.WriteLine("User Id is:"+ ViewBag.UserId);

        return View("RosterPageBasketball", d);
}

数据流:

问题可能出在哪里:RosterPageBasketball.cshtml

<div>
    @await Html.PartialAsync("_Displayeach", Model["bbM7and8"])
</div>

我尝试了什么(1):

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

结果:

这没什么区别。问题刚刚从@await Html.PartialAsync("_Displayeach", Model["bbM7and8"])@await Html.PartialAsync("_Displayeach", data)

问题似乎是我无法从传入的模型中找到 UserId。

我尝试了什么(2):

<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p
<p>@mmLeagueParticipant.child.Parent.UserId</p>

如果我<p>@mmLeagueParticipant.child.Parent.UserId</p>在我当前的 p 标签下添加;在浏览器中显示以下错误:

Object reference not set to an instance of an object.

浏览器不喜欢:

结果:何时<p>@mmLeagueParticipant.child.Parent.UserId</p>被注释掉

问题可能出在哪里(不确定): 我保存会话的位置在 HomeController 中。但是我试图调用它的地方是在我的 DashboardController 中。我更喜欢将登录和注册控制器与我的 DashboardController 分开。

家庭控制器:

HttpContext.Session.SetInt3b2("UserId", dbUser.UserId);
ViewBag.UserId = HttpContext.Session.GetInt32("UserId");
Console.WriteLine(ViewBag.UserId); //<<--this works fine and shows the correct Id when I'm login in.

模型描述和关系:


我的联赛模型:

int LeagueId { get; set; }
string sport { get; set; }
string gender { get; set; }
string ageRange { get; set; }

​ 列出所有参与者 { 获取;放; }


我的 ViewModel 模型:

public List<Participant> allParticipants { get; set; }
public ViewModel.Participant participant { get; set; }

public class Participant
{
    int ParticipantId { get; set; }
    string ParticipantFirstName { get; set; }
    string ParticipantLastName { get; set; }
    string ParticipantGender { get; set; }
    system.DateTime ParticipantDOB { get; set; }

    List<MMLeagueParticipant> allLeagues { get; set; }

    int UserId { get; set; }
    User Parent { get; set; }
}

我的 MMLeagueParticipant 中间表用于联赛模型和 ViewModel.Participant:

int MMLeaugeParticipantId { get; set; }
int ParticipantId { get; set; }
int LeaugeId { get; set; }
leauge sport { get; set; }

ViewModel.Participant child { get; set; }

我的用户模型:

int UserId { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string Email { get; set; }
string Password { get; set; }
strig ConfirmPassword { get; set; }

List<ViewModel.Participant> kids { get; set; }

标签: c#asp.net-core-mvcviewbag

解决方案


推荐阅读