【问题标题】:making drop down menu and tabs dynamic使下拉菜单和选项卡动态化
【发布时间】:2021-08-26 00:10:39
【问题描述】:

我有一个下拉菜单,在同一个 cshtml 页面上显示不同的选项卡。

  • 每个选项卡都显示一个篮球运动员列表。
  • 问题:所有选项卡都显示相同的列表。 (年龄组 7 和 8)。 9-10 选项卡和 11-12 选项卡应包含各自的列表。
  • 使用:下拉选项卡和 mvc-3 的引导程序

下拉选项卡:

    <div class="col-9">
    <div class="tab-content" id="v-pills-tabContent">
        <div class="tab-pane fade show active" id="v-pills-boy" role="tabpanel" aria-labelledby="v-pills-boy-tab">
            <nav>
                <ul class="nav nav-tabs nav-justified" id="nav-tab" role="tablist">
                    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">b-Age</a>
                    <li class="dropdown-menu">
                        <a class="dropdown-item nav-link" id="bbM7and8-tab" data-toggle="tab" href="#bbM7and8" role="tab" aria-controls="bbM7and8" aria-selected="false">7-8</a>
                        <a class="dropdown-item nav-link" id="bbM9and10-tab" data-toggle="tab" href="#bbM9and10" role="tab" aria-controls="bbM9and10" aria-selected="false">9-10</a>
                        <a class="dropdown-item nav-link" id="bbM11and12-tab" data-toggle="tab" href="#bbM11and12" role="tab" aria-controls="bbM11and12" aria-selected="false">11-12</a>
                        </li>
                </ul>
            </nav>
            <div class="tab-content" id="nav-tabContent">
                <div class="tab-pane fade" id="bbM7and8" role="tabpanel" aria-labelledby="bbM7and8-tab">
                    @{
                        <div>
                            <partial name="_Displayeach"/>
                        </div>
                    }
                <div class="tab-pane fade" id="bbM9and10" role="tabpanel" aria-labelledby="bbM9and10-tab">{...}</div>
                <div class="tab-pane fade" id="bbM11and12" role="tabpanel" aria-labelledby="bbM11and12-tab">{...}</div>
            </div>
        </div>

_Displayeach 部分:

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

我尝试了什么:

测试以确保标签是否显示不同的信息:我为每个标签添加了年龄组编号

7-8 //how I know each tab is different
@{
    <div>
            <partial name="_Displayeach"/>
    </div>
}

我认为我的问题可能在哪里:

由于每个选项卡都不同,这让我相信它可能与我的控制器以及我如何将列表传回有关。我对 mvc 框架还很陌生

    [HttpGet]
[Route("roster/basketball")]
public async Task<IActionResult> GetBasketballRoster()
{
    String[] leagueNames = new[]
    {
        "7and8",
        "9and10",
        "11and12"
    };
    List<League> bbLeagues = await db.Leagues
    .Where( l => l.sport == "Basketball" )
    .Where( l => leagueNames.Contains( l.ageRange ) )
    .ToListAsync();

    foreach( League league in bbLeagues )
    {
        List<MMLeagueParticipant> leagueParticipants = await db.Entry( league )
            .Collection( l => l.allParticipants )
            .Query() // <-- This is needed to allow for `Include()`
            .Include( mmp => mmp.child )
            .ToListAsync();
    }
    return View("RosterPageBasketball", bbLeagues);
}

public class RosterPageViewModel
{
    public RosterPageViewModel(IReadOnlyDictionary< String, League > leagues)
    {
        this.Leagues = leagues ?? throw new ArgumentNullException(nameof(leagues));
    }
    public IReadOnlyDictionary< String, League > Leagues { get; }
}

我的联赛模型:

int LeagueId { get; set; }
string sport { get; set; }
string gender { get; set; }
string ageRange { get; set; }
​List<MMLeagueParticipant> allParticipants  { get; set; }

我的 ViewModel 模型:

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; }
SystemDateTime ParticipantDOB { get; set; }
List<MMLeagueParticipant> all Leagues { get; set; }
}

我的 MMLeagueParticipant 中间表用于联赛模型和 ViewModel.Participant:

int MMLeaugeParticipantId { get; set; }
int ParticipantId { get; set; }
int LeaugeId { get; set; }
leauge sport { get; set; }
ViewModel.Participant child { get; set; }

【问题讨论】:

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


    【解决方案1】:

    在您的代码中,您错过了将legue 传递给每个标签页。 渲染局部是不够的,你还需要给每个局部一个特定的模型。

    1. 步骤是为您的视图定义一个 ViewModel

      public class LeagueViewModel
      {
          //model for each tab
          public List<League> Leagues { get; set; }
      }
      
    2. 在控制器中查询数据并返回视图模型

      public async Task<LeagueViewModel> Get(YourDbContext db, string[] leagueNames)
          {
              var leagues =await db.Legues
                  .Include(x => x.Child)
                  .Where(l => l.Sport == "Basketball")
                  .Where(l => leagueNames.Contains(l.AgeRange))
                  .ToListAsync();
      
              return new LeagueViewModel
              {
                  Leagues = leagues
              };
          }
      
    3. 然后在您的视图中,您可以循环 model.Legues 并显示部分

              foreach (var league in Model.Leagues)
              {
                  @await Html.PartialAsync("_PartialName", league)
              }
      

    【讨论】:

    • 我认为这可能有效。我更新了我的帖子以展示我的模型。我还不是最擅长在 mvc 中查询的。
    【解决方案2】:

    您可以尝试将Dictionary&lt;string, List&lt;League&gt;&gt; 传递给查看,并从字典中获取列表以使用leagueName 将其传递给部分视图。这是一个工作演示:

    行动:

    [HttpGet]
    [Route("roster/basketball")]
    public IActionResult GetBasketballRoster()
            {
                string[] leagueNames = new[]
                {
                    "7and8",
                    "9and10",
                    "11and12"
                };
                Dictionary<string, List<League>> d = new Dictionary<string, List<League>>();
                foreach (var name in leagueNames)
                {
                    List<League> bbLeagues = await db.Leagues
                    .Where(l => l.sport == "Basketball")
                    .Where(l => l.ageRange==name)
                    .ToListAsync();
    
                    foreach (League league in bbLeagues)
                    {
                        List<MMLeagueParticipant> leagueParticipants = await db.Entry(league)
                            .Collection(l => l.allParticipants)
                            .Query() // <-- This is needed to allow for `Include()`
                            .Include(mmp => mmp.child)
                            .ToListAsync();
                    }
                    d.Add(name,bbLeagues);
                }
               
                return View(d);
            }
    

    查看:

    @model Dictionary<string, List<League>>
    <div class="col-9">
        <div class="tab-content" id="v-pills-tabContent">
            <div class="tab-pane fade show active" id="v-pills-boy" role="tabpanel" aria-labelledby="v-pills-boy-tab">
                <nav>
                    <ul class="nav nav-tabs nav-justified" id="nav-tab" role="tablist">
                        <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">b-Age</a>
                        <li class="dropdown-menu">
                            <a class="dropdown-item nav-link" id="bbM7and8-tab" data-toggle="tab" href="#bbM7and8" role="tab" aria-controls="bbM7and8" aria-selected="false">7-8</a>
                            <a class="dropdown-item nav-link" id="bbM9and10-tab" data-toggle="tab" href="#bbM9and10" role="tab" aria-controls="bbM9and10" aria-selected="false">9-10</a>
                            <a class="dropdown-item nav-link" id="bbM11and12-tab" data-toggle="tab" href="#bbM11and12" role="tab" aria-controls="bbM11and12" aria-selected="false">11-12</a>
                        </li>
                    </ul>
                </nav>
                <div class="tab-content" id="nav-tabContent">
                    <div class="tab-pane fade" id="bbM7and8" role="tabpanel" aria-labelledby="bbM7and8-tab">
                        <div>
                            @await Html.PartialAsync("_Displayeach", Model["7and8"])
                        </div>
                    </div>
                    <div class="tab-pane fade" id="bbM9and10" role="tabpanel" aria-labelledby="bbM9and10-tab">
                        <div>
                            @await Html.PartialAsync("_Displayeach", Model["9and10"])
                        </div>
                    </div>
                    <div class="tab-pane fade" id="bbM11and12" role="tabpanel" aria-labelledby="bbM11and12-tab">
                        <div>
                            @await Html.PartialAsync("_Displayeach", Model["11and12"])
                        </div>
                    </div>
                </div>
               
            </div>
        </div>
    </div>
    

    【讨论】:

    • CMD: "不能从 'string' 转换为 'int'" 对于每个 @await Html.PartialAsync("_Displayeach", Model["#and#"])
    • 你项目中字典的key类型是string?代码在我的项目中有效。
    • 我可能有错字,再试一次,现在它不喜欢这个视图。终端错误:InvalidOperationException:未找到视图“GetBasketballRoster”
    • 我项目中的视图名称是GetBasketballRoster
    • 我将返回行更改为 "return View("RosterPageBaseball", d);"我得到的错误:InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[LeagueProject.Models.League]]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[LeagueProject.Models.League]'.`
    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多