c# - 根据登录的用户 ID 分隔列表
问题描述
我有League
我传递给我的视图的列表。然后,视图应确定 的 id 是否与已登录Participant
的 id 匹配;谁的 ID 保存在.UserId
User
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>
}
}
问题:
<p>Everybody elses kid(s):</p>
无论 UserId 是否匹配,整个列表都显示在下方。而且没有任何东西<p>Your kid(s):</p>
错误:
NullReferenceException: Object reference not set to an instance of an object.
然后我查看了我的控制器,看看它是如何返回为空的。
控制器详细信息:
在我的控制器中,我有一个添加新
participant
模型的年龄组列表。然后我将列表传递给我的
RosterPageBasketball
视图。在该视图中,我有 12 个选项卡;每个都根据年龄组显示不同的列表。
RosterPageBacketball 中使用的模型:
@model List<League>
RosterPageBasketball 中显示不同年龄组的每个选项卡:
@await Html.PartialAsync("_Displayeach", Model["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<string, List> 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>@mmLeagueParticipant.child.Parent.UserId</p>
在我当前的 p 标签下添加;在浏览器中显示以下错误:
Object reference not set to an instance of an object.
浏览器不喜欢:
- @mmLeagueParticipant.child.Parent.UserId
- @await Html.PartialAsync("_Displayeach", 数据)
结果:何时<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.Participant
并League
共享一张MMLeagueParticipant
中间桌子
我的联赛模型:
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; }
解决方案
推荐阅读
- javascript - 如何在 AMP 页面中获取 document.referrer
? - list - 如何将两个数据放在一个标题中?
- php - 一次性支付网页wordpress
- batch-file - 用于整理文件夹的简单批处理脚本
- jsf - 无法使用 primefaces 上传文件上传文件
- android - 如何在 Kotlin 中创建对象列表?
- swift - 在 stringdict 文件中使用 \n
- reactjs - 在使用 YUP 的电子邮件验证中只允许 ASCII 字符
- python - sklearn.exceptions.NotFittedError:此管道实例尚未安装。在使用此估算器之前使用适当的参数调用“fit”
- mulesoft - 如何与 mulesoft 流设计器一起睡觉