【发布时间】:2021-08-17 03:58:00
【问题描述】:
我有一个包含运动列表的模型。但是在我创建第一个之前,我试图调用的对象还不存在。所以它在“LeaguebbM7and8”下给了我红色的波浪线我相信我正在查询它完全错误,因为我是 C# 新手。
错误显示:
'League' does not contain a definition for 'LeaguebbM7and8' and no accessible extension method 'LeaguebbM7and8' accepting a first argument of type 'League' could be found
我的联赛模型:
- int LeagueId { 获取;放; }
- 弦乐运动{得到;放; }
- 字符串性别{获取;放; }
- 字符串年龄范围{获取;放; }
- 列表
allParticipants { get;放; }(联盟和参与者的多对多关系)
我的 ViewModel 模型:
- ViewModelParticipant 参与者 { 获取;放; }
- 列表 allParticipants { 获取;放; }
- 公开课参与者
- int ParticipantId { 获取;放; }
- 字符串 ParticipantFirstName { 获取;放; }
- 字符串 ParticipantLastName { 获取;放; }
- 字符串 ParticipantGender { 获取;放; }
- SystemDateTime 参与者DOB { get;放; }
- 列出
所有联赛{get;放; }
我的联赛模型和 ViewModel.Participant 的 MMLeagueParticipant 中间表:
- int MMLeugeParticipantId { 获取;放; }
- int ParticipantId { 获取;放; }
- int LeaugeId { 获取;放; }
- leauge 运动 { 得到;放; }
- ViewModel.Participant 孩子 { 获取;放; }
以下是我在页面上显示信息的方式:
@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>
我想做什么:
- 显示特定年龄组参与者的所有名字和姓氏(如果存在)。如果不是我想显示“空列表”
我不确定这是否与我创建联盟的方式有关。以下是我的做法。
我添加新联盟的方式:
-
从某种形式传入一个 ID
-
如果该 ID 存在,它将检查性别和年龄
-
根据这些检查,控制器会将其添加到现有联赛或创建联赛然后添加(如果联赛尚不存在)
[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"); }
以下是我如何调用烘焙页面并将列表作为模型发送: 列表将是所有将篮球作为我的运动项目的联赛。
[HttpGet]
[Route("roster/basketball")]
public IActionResult BasketballRoster()
{
List<League> allBasketballLeagues = db.Leagues.Where(i=>i.sport =="Basketball").ToList();
return View("RosterPageBasketball", allBasketballLeagues);
}
【问题讨论】:
-
嗨@jgrewal,欢迎来到 StackOverflow。您能否确认您的错误消息不是
'List<League>' does not contain a definition for 'LeaguebbM7and8' and no accessible extension method 'LeaguebbM7and8' accepting a first argument of type 'List<League>' could be found。
标签: c# asp.net-mvc asp.net-core