Razor Foreach只会显示IEnumerable集合中的最后一项

时间:2018-01-05 12:28:05

标签: c# asp.net-mvc razor

我的视图中有以下代码,它应该迭代一个集合,我应该看到14个具有不同名称的图像。但它只会显示集合中的最后一项。

我用来显示图像的代码

        IEnumerable<Sector> sector = ViewBag.Sectors;

    <div id="js-grid-lightbox-gallery" class="cbp cbp-ready cbp-caption-active cbp-caption-overlayBottomAlong" style="height: 896px; margin:.5px 0px 0px 50px">
    <div class="cbp-wrapper-outer">
        <div class="cbp-wrapper">     
            @foreach (var item in sector)
            {
                <div class="cbp-item web-design graphic print motion" style="width: 389px; left: 0px; top: 0px;">
                    <div class="cbp-item-wrapper" style="">
                        <a href="ajax-lightbox-gallery/project1.html" class="cbp-caption cbp-singlePageInline" data-title="World Clock<br>by Paul Flavius Nechita" rel="nofollow">
                            <div class="cbp-caption-defaultWrap">
                                <img src="https://placeimg.com/380/285/arch" alt="">
                            </div>
                            <div class="cbp-caption-activeWrap">
                                <div class="cbp-l-caption-alignLeft">
                                    <div class="cbp-l-caption-body">
                                        <div class="cbp-l-caption-title">@item.Name</div>                                      
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
            }                                         
        </div>
    </div>
</div>

我用来获取数据的代码。

     public IActionResult Experience(string sectorid)
    {

        IEnumerable<Sector> sector = sectorRepository.AllActive().ToList();
        ViewBag.Sectors = sector;

        return View();
    }

我看不出我做错了什么。我已经尝试了这个,我仍然只得到一个图像。

    <div id="js-grid-lightbox-gallery" class="cbp cbp-ready cbp-caption-active cbp-caption-overlayBottomAlong" style="height: 896px; margin:.5px 0px 0px 50px">
    <div class="cbp-wrapper-outer">
        <div class="cbp-wrapper">     
            @foreach (var item in Model)
            {
                <div class="cbp-item web-design graphic print motion" style="width: 389px; left: 0px; top: 0px;">
                    <div class="cbp-item-wrapper" style="">
                        <a href="ajax-lightbox-gallery/project1.html" class="cbp-caption cbp-singlePageInline" data-title="World Clock<br>by Paul Flavius Nechita" rel="nofollow">
                            <div class="cbp-caption-defaultWrap">
                                <img src="https://placeimg.com/380/285/arch" alt="">
                            </div>
                            <div class="cbp-caption-activeWrap">
                                <div class="cbp-l-caption-alignLeft">
                                    <div class="cbp-l-caption-body">
                                        <div class="cbp-l-caption-title">@item.Name</div>                                      
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
            }                                         
        </div>
    </div>
</div>

public IActionResult Experience(string sectorid)
    {

        IEnumerable<Sector> sector = sectorRepository.AllActive().ToList();


        return View(sector);
    }

0 个答案:

没有答案