Introduction
The image slide, using Bootstrap in ASP.NET MVC is called Image Carousel.
Description
The Carousel plugin is a component for cycling through the elements like a carousel (slideshow).
carousel
Creates a carousel
slide
Adds sliding animation effect when transitioning from one item to the other.
carousel-inner
This class is applied on the element, which contains all the slides of the carousel.
item
Specifies the conent of each slide. Content can be text and images.
carousel-caption
Specifies a caption for the carousel.
carousel-indicators
Adds the dot indicators at the bottom of each slide, which indicates the current slide the user is viewing and the the total number of slides.
carousel-control
Adds the left or right buttons on the carousel to go back or forward one slide. To add left button, use left class along with carousel-control class and to add right button use right class along with carousel-control.
data-interval
Specifies the time delay in milli-seconds for transitioning from one slide to another. Set this attribute to false, if you do not want automatic sliding.
data-pause
The default value is hover. Pauses from transitioning to the next slide on hover. Set this attribute to false to stop the ability to pause on hover.
data-wrap
The default value is true, which means the carousel transitions through all the slides without stopping at the last slide. To stop at the last slide, set this attribute to false.
data-ride="carousel"
It activates the carousel. The carousel can also be manually activated by using JavaScript.
$("#carousel_Id").carousel();
data-slide-to
Specifies which slide to go to when clicked. Slide index is ZERO based.
data-slide
This attribute is added to the carousel controls (LEFT and Right buttons). For the left button, the value is "prev" and for the right button, the value is "next".
Steps
Create a MVC Application named SatyaMVCBootstrapImages.
Add bootstrap folder from the site, which I had already described in my previous blog.
Add 4 images in Images folder.
Add an action result method named "carousel()" in Home controller.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace SatyaMVCBootstrapImages.Controllers
- {
- public class HomeController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult ModalPopUp()
- {
- return View();
- }
- public ActionResult carousel()
- {
- return View();
- }
-
- }
- }
Add a view called carousel.cshtml.
- @{
- ViewBag.Title = "carousel";
- }
-
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Satyaprakash Carousel</title>
- <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
-
- <h2 style="background-color: Yellow;color: Blue; text-align: center; font-style: oblique">Satyaprakash's Image Carousel</h2>
- <fieldset>
- <legend style="font-family:Arial Black;color:blue">Company Founder Using Image Carousel</legend>
- <div class="container">
- <div class="row">
- <div class="col-md-8 col-md-offset-2">
- <div id="imageCarousel" class="carousel slide" data-interval="2000"
- data-ride="carousel" data-pause="hover" data-wrap="true">
-
- <ol class="carousel-indicators">
- <li data-target="#imageCarousel" data-slide-to="0" class="active"></li>
- <li data-target="#imageCarousel" data-slide-to="1"></li>
- <li data-target="#imageCarousel" data-slide-to="2"></li>
- <li data-target="#imageCarousel" data-slide-to="3"></li>
- </ol>
-
- <div class="carousel-inner">
- <div class="item active">
- <img src="Images/2.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Mahesh Chand's S/W Developer Community</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/4.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Bill Gates's Software Company</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/5.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Larry Page's Search Engine Company</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/6.jpg" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Jack Dorsey's Social Networking Company</p>
- </div>
- </div>
- </div>
-
- <a href="#imageCarousel" class="carousel-control left" data-slide="prev">
- <span class="glyphicon glyphicon-hand-left"></span>
- </a>
- <a href="#imageCarousel" class="carousel-control right" data-slide="next">
- <span class="glyphicon glyphicon-hand-right"></span>
- </a>
- </div>
-
- </div>
- </div>
- </div>
- </fieldset>
- <footer>
- <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@
- </footer>
-
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
- </script>
- <script src="bootstrap/js/bootstrap.min.js"></script>
Here, I added the code for image slide time interval and subsequently, I added how can an image will pause slide during slide operation, which is going on.
- <div id="imageCarousel" class="carousel slide" data-interval="2000"
- data-ride="carousel" data-pause="hover" data-wrap="true">
Here, I added some dot operators, which indicates the current slide the user is viewing and the the total number of slides.
- <ol class="carousel-indicators">
- <li data-target="#imageCarousel" data-slide-to="0" class="active"></li>
- <li data-target="#imageCarousel" data-slide-to="1"></li>
- <li data-target="#imageCarousel" data-slide-to="2"></li>
- <li data-target="#imageCarousel" data-slide-to="3"></li>
- </ol>
Here, I added four images with image description and the classes, which were defined above. All the images should have same dimensions.
- <div class="carousel-inner">
- <div class="item active">
- <img src="Images/2.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Mahesh Chand's S/W Developer Community</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/4.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Bill Gates's Software Company</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/5.png" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Larry Page's Search Engine Company</p>
- </div>
- </div>
- <div class="item">
- <img src="Images/6.jpg" class="img-responsive">
- <div class="carousel-caption">
- <h3></h3>
- <p style="font-weight: bold; color:yellow;font-size:small">Jack Dorsey's Social Networking Company</p>
- </div>
- </div>
- </div>
To add left button, use left class along with carousel-control class and to add right button, use right class along with carousel-control.
- <a href="#imageCarousel" class="carousel-control left" data-slide="prev">
- <span class="glyphicon glyphicon-hand-left"></span>
- </a>
- <a href="#imageCarousel" class="carousel-control right" data-slide="next">
- <span class="glyphicon glyphicon-hand-right"></span>
- </a>
Output
Desktop view
dot mark :
left and right
image details
all 4 images
Mobile view with dot mark, left and right, image details
SUMMARY
- What is an Image Carousel.
- Apply In ASP.NET MVC.
- All the related classes and attributes.
- All the images shoud have the same dimensions.