【发布时间】:2018-10-01 11:15:12
【问题描述】:
我尝试为来自 SQL Server 数据库的新闻制作 Bootstrap 轮播滑块
我希望滑块看起来像这样:https://www.w3schools.com/bootstrap/bootstrap_carousel.asp
但是角色说
.active 类需要添加到其中一张幻灯片中。否则,轮播将不可见。
但我不知道如何将 .active 类用于我的新闻之一,因为它是 foreach 循环。
我所有的新闻也都在互相下,左右键不起作用
Here is a screenshot of news slider result now
现在的代码:
HomeController.cs
public ActionResult Index()
{
return View(db.News.ToList());
}
索引.cshtml
@model IEnumerable<Project.Models.News>
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Scripts.Render("~/bundles/bootstrap")
<!--Start slide code-->
<div class="container imgs">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
@foreach (var item in Model)
{
<div class="carousel-inner">
<div class="item active"> //My mistake is here, how to make first news of class active?
<div class="item ">
@{ string imageBase64 = Convert.ToBase64String(item.newImg);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
<img src="@imageSrc" />
}
<div class="carousel-caption">
<h3>@item.newName</h3>
<a href="@Url.Action("News", "News", new { id = item.newId })" class="btn btn-default" style="background-color:white ;color:black ">Read More</a>
@*<button type="button" class="btn btn-default">Read More</button>*@
</div>
</div>
</div>
</div>
}
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
有什么帮助吗?
【问题讨论】:
标签: c# sql-server asp.net-mvc slider slideshow