Reputation: 342
I have a list model being passed into my view. But I want to separate the Participant
's inside the list depending on which User
is logged in on the ViewBag.
how I'm saving the logged in user to ViewBag from controller:
HttpContext.Session.SetInt32("UserId", dbUser.UserId);
ViewBag.UserId = HttpContext.Session.GetInt32("UserId");
current view code:
@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>
}
}
my code currently works but it is displaying all the Participant
's regardless of if they belong the to current logged in User
or not.
Solution I thought that might work:
<p>Your kids:</p>
foreach(League i in Model)
{
foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
{
if(mmLeagueParticipant.child.Parent.UserId == ViewBag.UserId )
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
}
<p>All other kids:</p>
foreach(League i in Model)
{
foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
{
if(mmLeagueParticipant.child.Parent.UserId != ViewBag.UserId )
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
}
The error I get from trying this:
NullReferenceException: Object reference not set to an instance of an object.
it doesn't like the if(mmLeagueParticipant.child.Parent.UserId == ViewBag.UserId)
line.
Models relations:
league
model and ViewModel
(Participant
) share a MMLeagueParticipant
middle table for a many to many relationships (one Participant
can play many League
's and many League
's can have many Participant
's)User
model has a one-to-many relationships with the ViewModel
. (one Logged in User
can have many Participant
's)My league model:
int LeagueId { get; set; }
string sport { get; set; }
string gender { get; set; }
string ageRange { get; set; }
List<MMLeagueParticipant> 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; }
Upvotes: 1
Views: 239
Reputation: 103
You can simply filter your participants with
var userParticipants = allParticipants.Where(p => p.UserId == Viewbag.UserId).ToList()
It requires you to use the System.Linq namespace.
Edit: Here's a more specific example
foreach(League i in Model)
{
var userParticipants = i.allParticipants.Where(p => p.UserId == Viewbag.UserId).ToList();
foreach (MMLeagueParticipant mmLeagueParticipant in userParticipants)
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
Upvotes: 2