【问题标题】:query through a database and displaying on cshtml通过数据库查询并在cshtml上显示
【发布时间】: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&lt;League&gt;' does not contain a definition for 'LeaguebbM7and8' and no accessible extension method 'LeaguebbM7and8' accepting a first argument of type 'List&lt;League&gt;' could be found

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

问题

在您的页面中,您的ModelList 类型。因此您无法从List 访问LeaguebbM7and8

@model List<League>

解决方案

  1. 你应该通过这种方式检查 List is not null and not Empty
Model != null && Model.Any()
  1. [已更新]由于您的 League 类包含 allParticipants 这是 List&lt;MMLeagueParticipant&gt; 类型,因此您必须使用 foreach 循环来访问每个 MMLeagueParticipant 元素。
foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)

您的 .cshtml 页面应该是:

@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>

仅供参考,

  1. IEnumerable对象至少有1个元素时,.Any()返回true,相当于.Count() &gt; 0
  2. else if 切换到else,因为它验证Model 既不是null 也不是空列表。如果未能满足if 语句,它将继续执行else 语句。

【讨论】:

  • 我在查询和显示名字和姓氏时仍然遇到问题。我试过@i.LeaguebbM7and8.allParticipants.child.ParticipantFirstName
  • 不,你不能那样做。你必须使用@i.allParticipants.child.ParticipantFirstNameiLeague 类型,您的 League 类不包含 LeaguebbM7and8 属性。
  • 子部分给了我以下错误'List'不包含'child'的定义并且没有可访问的扩展方法'child'接受'List类型的第一个参数'可以找到'。即使我的 MMLeagueParticcipants.cs 中有孩子。另一个问题是我只想显示 7 和 8 年龄段的参与者。我在想我应该通过创建新联赛的数据库而不是 foreach(模型中的联赛 i)来查询。不知道该怎么做。
  • 嗨@jgrewal,对不起,我已经更新了我的答案,因为你的allParticipantsList&lt;MMLeagueParticipant&gt; 类型,你必须使用foreach 循环来检索每个MMLeagueParticipant 对象。
  • 对于您的第二期,您可以使用.Where()BasketballRoster 方法中过滤ageRange 以获得所需的条件。
猜你喜欢
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
相关资源
最近更新 更多