简体   繁体   中英

query through a database and displaying on cshtml

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:

  • int LeagueId { get; set; }
  • string sport { get; set; }
  • string gender { get; set; }
  • string ageRange { get; set; }
  • List<MMLeagueParticipant> allParticipants { get; set; } (a many to many relationship for league and participant)

my ViewModel model:

  • ViewModelParticipant participant { get; set; }
  • List<Participant> allParticipants { get; set; }
  • public class Participant
    • int ParticipantId { get; set; }
    • string ParticipantFirstName { get; set; }
    • string ParticipantLastName { get; set; }
    • string ParticipantGender { get; set; }
    • SystemDateTime ParticipantDOB { get; set; }
    • List<MMLeagueParticipant> all Leagues { 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; }

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:

  • display all the first and lastnames of the the participants in certain age group if it exists. if it doesn't i want to display "empty list"

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

ISSUE

In your page, your Model is List type. Thus you cannot access LeaguebbM7and8 from List .

@model List<League>

SOLUTION

  1. You should do in this way to check List is not null and not Empty
Model != null && Model.Any()
  1. [UPDATED] As your 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,

  1. .Any() return true when the IEnumerable object has at least 1 element, it is equivalent to .Count() > 0 .
  2. Switch 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM