在for循环中使用变量I在for循环中声明的值

本文关键字:循环 for 声明 变量 | 更新日期: 2023-09-27 18:17:19

我有一个for循环来重复表头,每个表头都有一个下拉菜单。下拉菜单有href,我需要传递href,它将发送数据,如visit1=item, visit2=item

我不想硬录visit1, visit2,…等等,它应该出现在for循环

        @for (var i = 1; i < ViewBag.NoOfVisits + 1; i++)
                {
                    <th>
                        <p>Visit @i</p>
                        <div class="dropdown pull-right">
                            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" role="menu">
                                @foreach (var item in ViewBag.visit)
                                {
                                    <li role="presentation"><a role="menuitem" tabindex="-1" href="@Url.Action("Index", "ClinicalRegistryManager", new {siteId =@ViewBag.siteId, visit+@i = item})">@item</a></li>
                                }

                            </ul>
                        </div>
                    </th>
                }

看到第一个下拉列表了吗,在下拉列表中我有另一个foreach,在里面你可以看到一个带有href的li它将尝试发送visit+@i=item,它不起作用,

  new {siteId =@ViewBag.siteId, visit+@i = item})

那么我应该做些什么来克服这个呢?

在for循环中使用变量I在for循环中声明的值

我是这样做的…有人只是检查一下,然后改进一下?

                @for (var i = 1; i < ViewBag.NoOfVisits + 1; i++)
                {
                    <th>
                        <p>Visit @i</p>
                        <div class="dropdown pull-right">
                            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" role="menu">
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="@Url.Action("Index", "ClinicalRegistryManager", new { siteId = @ViewBag.siteId })">Show all</a></li>
                                @{
                                    var array = ViewBag.visit1;
                                    switch (i)
                                    {
                                        case 1:
                                            {
                                                array = ViewBag.visit1;
                                                break;
                                            }
                                        case 2:
                                            {
                                                array = ViewBag.visit2;
                                                break;
                                            }
                                        case 3:
                                            {
                                                array = ViewBag.visit3;
                                                break;
                                            }
                                        case 4:
                                            {
                                                array = ViewBag.visit4;
                                                break;
                                            }
                                        case 5:
                                            {
                                                array = ViewBag.visit5;
                                                break;
                                            }
                                    }
                                }
                                @foreach (var item in array)
                                {
                                    var visit = "visit" + @i;
                                    <li role="presentation"><a role="menuitem" tabindex="-1" href="~/ClinicalRegistryManager/Index?SiteId=@ViewBag.siteId&@visit=@item">@item</a></li>
                                }

                            </ul>
                        </div>
                    </th>
                                    }