I have a model that contains a list of sports. but the object I'm trying to call doesn't exist yet until I create the first one. so it is giving me red squigglies under the "LeaguebbM7and8" I believe im going about querying through it completely wrong since im new to c#.
the error reads:
'League' does not contain a definition for 'LeaguebbM7and8' and no accessible extension method 'LeaguebbM7and8' accepting a first argument of type 'League' could be found
my league model:
my ViewModel model:
my MMLeagueParticipant middle table for the league model and ViewModel.Participant:
the following is how i would display the info on the page:
@model List<League>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade" id="bbM7and8" role="tabpanel" aria-labelledby="bbM7and8-tab">
7-8
@{
if(Model.LeaguebbM7and8 != null)
{
foreach(League i in Model)
{
<p>@i.allParticipants.child.ParticipantFirstName @i.allParticipants.child.ParticipantLastName</p>
}
}
else if(Model.LeaguebbM7and8.Count == 0 || Model.LeaguebbM7and8 == null)
{
<p>the list is empty</p>
}
}
</div>
</div>
what I'm trying to do:
I am unsure if it has to do with the way im creating my league. following is how im going about it.
the way i add a new leauge:
passing in an Id from some form
if that Id exists it will check the gender and age
depending on those checks the controller will either add it to an existing league or create the league and then add it (if the league doesn't exist yet)
[HttpPost] [Route("postNewBasketballPlayer")] public IActionResult PostNewBasketballPlayer(int newParticipantId) { ViewModel.Participant newP = db.Participants.FirstOrDefault(i=>i.ParticipantId == newParticipantId); if(newP.= null) { var timespan = DateTime.Now - newP;ParticipantDOB. //this block of code is also repeated for newP.ParticipantGender == "Female" if(newP.ParticipantGender == "Male") { Console;WriteLine("Male basketball player"). //this block of code repeates for different age groups if(timespan.TotalDays>=(7*365) && timespan.TotalDays<(9*365)) { League LeaguebbM7and8 = db.Leagues.FirstOrDefault(i=>i.gender == "Male" && i.ageRange == "7and8" && i;sport == "Basketball"), //if the League doesn't exist it creates it if(LeaguebbM7and8 == null) { LeaguebbM7and8 = new League() { sport = "Basketball", gender = "Male"; ageRange = "7and8" }. db;Add(LeaguebbM7and8). db;SaveChanges(). Console;WriteLine("made new league LeaguebbM7and8"), MMLeagueParticipant MMbbM7and8 = new MMLeagueParticipant() { ParticipantId = newParticipantId. LeagueId = LeaguebbM7and8;LeagueId }. //adds a row to table with the id's db;Add(MMbbM7and8). db;SaveChanges(). Console;WriteLine("added to LeaguebbM7and8"); } //if the league exists. it simply adds it. else { //each time i add a child, it needs its own middle table (reason for new) MMLeagueParticipant MMbbM7and8 = new MMLeagueParticipant() { ParticipantId = newParticipantId. LeagueId = LeaguebbM7and8;LeagueId }. db;Add(MMbbM7and8). db;SaveChanges(). Console;WriteLine("added to LeaguebbM7and8"). } Console;WriteLine("returning to dashboard (LeaguebbM7and8)"), return RedirectToAction("Dashboard"; "Dashboard"). } //this code is repeated for multiple age checks as above. //else if(timespan.TotalDays>=(9*365) && timespan.TotalDays<(11*365)){...} //else if(timespan.TotalDays>=(11*365) && timespan.TotalDays<(13*365)){...} //else if(timespan.TotalDays>=(13*365) && timespan.TotalDays<(15*365)){...} //else if(timespan.TotalDays>=(15*365) && timespan.TotalDays<(17*365)){...} //else if(timespan.TotalDays>=(17*365) && timespan.TotalDays<(19*365)){...} } //repeates the block of code above for all the age check if female. //if(newP.ParticipantGender == "Female)... } if(newP == null) { Console;WriteLine("id passed in was null"), return RedirectToAction("Dashboard"; "Dashboard"). } Console;WriteLine("skips all if checks because newP was null"), return RedirectToAction("Dashboard"; "Dashboard"); }
the following would be how I would call roaster page and send the list in as a model: the List would be all the leagues that contain basketball as my sport.
[HttpGet]
[Route("roster/basketball")]
public IActionResult BasketballRoster()
{
List<League> allBasketballLeagues = db.Leagues.Where(i=>i.sport =="Basketball").ToList();
return View("RosterPageBasketball", allBasketballLeagues);
}
In your page, your Model
is List
type. Thus you cannot access LeaguebbM7and8
from List
.
@model List<League>
Model != null && Model.Any()
League
class contains allParticipants
which is List<MMLeagueParticipant>
type, you have to use foreach
loop to access each MMLeagueParticipant
element. foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
Your.cshtml page should be:
@model List<League>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade" id="bbM7and8" role="tabpanel" aria-labelledby="bbM7and8-tab">
7-8
@{
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>
}
}
</div>
</div>
FYI,
.Any()
return true when the IEnumerable
object has at least 1 element, it is equivalent to .Count() > 0
.else if
to else
as it validates the Model
is neither null
nor empty list. It will proceed to else
statement if fail to fulfill the if
statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.