根据登录用户 ID 分隔列表

问题描述

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

如果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>
    }
}

问题:

无论 UserId 是否匹配,整个列表都显示<p>Everybody elses kid(s):</p> 下。 <p>Your kid(s):</p>

下没有任何内容

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

控制器详情:

  • 在我的控制器中,我有一个年龄组列表,我向其中添加了新的 participant 模型。

  • 然后我将列表传递给我的 RosterPageBasketball 视图。

  • 在该视图中,我有 12 个标签;每个都根据年龄组显示不同的列表。

  • RosterPageBacketball 中使用的模型:@model List<League>

  • RosterPageBasketball 中显示不同年龄组的每个标签

    @await Html.PartialAsync("_displayeach",模型["bbM7and8"])

我的 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);
}

数据流:

  • Dictionary d 传递给 RosterpageBasketball
  • RosterpageBasketball 使用 @model Dictionary<string,List<League>>
  • _displayeach 视图是传递一个模型["bbM#and#"]
  • _displayeach 视图使用 @model List<League>

问题可能出在哪里: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 标签添加 <p>@mmLeagueParticipant.child.Parent.UserId</p>;浏览器显示如下错误

Object reference not set to an instance of an object.

浏览器不喜欢:

  • @mmLeagueParticipant.child.Parent.UserId
  • @await Html.PartialAsync("_displayeach",data)

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

  • 在浏览器 <p>userId is: @viewBag.UserId</p>显示正确的 ID
  • 参与者的名字和姓氏根据需要出现在屏幕上

问题可能出在哪里(不确定): 我保存会话的地方是在 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.

模型描述和关系:

  • 一对多 - 一个 User 有多个 viewmodel.Participant
  • 多对多 - viewmodel.ParticipantLeague 共享一个 MMLeagueParticipant 中间表

我的联赛模式:

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

列出所有参与者 { get;放; }


我的 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; }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)