如何使用查询在foreach中实现显示数据的foreach?

时间:2015-12-07 11:19:22

标签: c# asp.net-mvc css3 razor foreach

我正在ASP.Net MVC5使用Razor engine,我希望在另一个foreach中使用foreach

@foreach(var item in Model.Table1)
 {
  ....
    @foreach(var item in Model.Table2)
    {
     ...Bootstrap Carousel 
    }
}

foreach显示表1中的所有项目,但foreach内部显示表2中的一些图像。 我不知道怎么做:/

这是我的代码,但它不起作用:

HTML:

@foreach (var item in Model.SubGoods)
{
<div style="background-color: #f5f5f5; text-align: center;padding:0px; " class="col-xs-12 col-sm-6 col-md-4 col-lg-3 text-center HoverBorder">
    <div style="padding:10px;background-color:white;">
        <div style="background-color: #f5f5f5; border: 3px solid #f5f5f5;">
            <a href="@Url.Action("GoodDetails", "Home", new { id = @item.SubGoodID })">
                <img class="img-responsive" src="@Url.Content(item.SubGoodImage.ToString())" />
            </a>
        </div>

        <div style="border: 3px solid #f5f5f5;">
            <h5 class="h">modelNumber : @item.SubGoodModelNumber</h5>
            <h5 class="h">Type : @item.SubGoodGender</h5>

            <hr style="background-color: #f5f5f5; height: 0.5px; width: 100%" />


            <h5 class="h">@item.SubGoodName</h5>

            <h5 class="h">@item.SubGoodInfo</h5>

            <h5 class="h div2"> @item.NishtmanCode</h5>
            <span class="div1">:ProductCode</span>
        </div>
    </div>
</div>
//----------------------

<div class="col-md-6 col-md-offset-3">
    <div class="carousel slide" id="myCarousel">
        <div class="carousel-inner">
            @foreach (var item1 in Model.GoodDetails)
            {

                if (item1 == Model.GoodDetails.FirstOrDefault())
                {
                    <div class="item active">
                        <div class="col-xs-3"><a href="#"><img style="width:40px;height:40px;" src="@item1.DetailsImage" class="img-responsive"></a></div>
                    </div>
                 }
               else
                {
                    <div class="item">
                        <div class="col-xs-3"><a href="#"><img style="width:40px;height:40px;" src="@item1.DetailsImage" class="img-responsive"></a></div>
                    </div>
                }
          </div>
                <a class="left carousel-control" href="#myCarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
                <a class="right carousel-control" href="#myCarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
      </div>
   </div>
//----------------------

}

另外,我不知道some images in inside foreach 的写入查询 HomeController:

 public ActionResult Goods(int id)
    {
        HomeVM vm = new HomeVM();
        vm.SocialNetworks = blSocialNetwork.select();
        var goods = db.SubGoods.Where(p => p.FKMainGoodID == id).ToList();
        vm.SubGoods = goods;
        return View(vm);
    }

我想要这样的事情: enter image description here

0 个答案:

没有答案