I have list of League's I pass to my View. The view should then determine whether or not the Participant's id matches the UserId for the User that is logged in; who's Id is saved in ViewBag.
If the id matched I want to display the list under the <p>Your kid(s):</p>; else the Participant's will be displayed under the <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>
}
}
issues:
The entire list is being displayed under the <p>Everybody elses kid(s):</p> regardless of if the UserId matching or not. And nothing under the <p>Your kid(s):</p>
error:
NullReferenceException: Object reference not set to an instance of an object.
I then looked at my controller to see how it may be coming back as null.
Controller details:
in my controller I have a list of age groups that i add new
participantmodels to.Then I pass the list to my
RosterPageBasketballview.Inside that view I have 12 tabs; each displaying different lists depending on the age group.
model used in RosterPageBacketball:
@model List<League>Each tab in the RosterPageBasketball that displays different agegroups:
@await Html.PartialAsync("_Displayeach", Model["bbM7and8"])
each tab in my RosterPageBasketballcontains the following code: @await Html.PartialAsync("_Displayeach", Model["bbM#and#"])
DashboardController:
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);
}
Flow of data:
- Dictionary<string, List> d is passed to the
RosterpageBasketball RosterpageBasketballuses@model Dictionary<string, List<League>>- _Displayeach view is passes a model["bbM#and#"]
- _Displayeach view uses
@model List<League>
where the issue might be:RosterPageBasketball.cshtml
<div>
@await Html.PartialAsync("_Displayeach", Model["bbM7and8"])
</div>
What I tried (1):
var data = Model.Where(a => a.Key == "bbF17and18").Select(a => a.Value).FirstOrDefault();
<div>
@await Html.PartialAsync("_Displayeach", data)
</div>
Results:
This made no difference. The issue just went from @await Html.PartialAsync("_Displayeach", Model["bbM7and8"]) to @await Html.PartialAsync("_Displayeach", data).
The problem seems to be I can't locate the UserId from the model being passed in.
What I tried (2):
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p
<p>@mmLeagueParticipant.child.Parent.UserId</p>
If I Add <p>@mmLeagueParticipant.child.Parent.UserId</p> under my current p tag; the following error is displayed in browser:
Object reference not set to an instance of an object.
browser doesn't like:
- @mmLeagueParticipant.child.Parent.UserId
- @await Html.PartialAsync("_Displayeach", data)
Results: when <p>@mmLeagueParticipant.child.Parent.UserId</p> is commented out
- In the browser
<p>userId is: @viewBag.UserId</p>the correct Id is displayed - The first and last names of the participants were appearing on the screen as they needed to
Where the issue might be(Uncertain): The place I am saving my Session is in HomeController. However the place I'm trying to call it is in my DashboardController. I do prefer to keep the log-in and register controller separate from my DashboardController.
HomeController:
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.
model description and relationships:
- one to many - one
Userhas manyViewModel.Participant's - many to many -
ViewModel.ParticipantandLeagueshare aMMLeagueParticipantmiddle table
My league model:
int LeagueId { get; set; }
string sport { get; set; }
string gender { get; set; }
string ageRange { get; set; }
ā List allParticipants { get; set; }
My ViewModel model:
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; }
}
My MMLeagueParticipant middle table for the league model and ViewModel.Participant:
int MMLeaugeParticipantId { get; set; }
int ParticipantId { get; set; }
int LeaugeId { get; set; }
leauge sport { get; set; }
ViewModel.Participant child { get; set; }
My User model:
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; }