Populating Items in ASP.NET MVC Carousel control

8 Aug 202319 minutes to read

In the Carousel, slides can be rendered in two ways as follows,

  • Populating items using carousel item
  • Populating items using data source

When rendering the Carousel component using items binding, you can assign templates for each item separately or assign a common template to each item. You can also customize the slide transition interval for each item separately. The following example code depicts the functionality as item property binding.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").Items(new List<CarouselItem> {
                    new CarouselItem { Template = "<div class='slide-content'>Slide 1</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 2</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 3</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 4</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 5</div>"  }
                })
                .Render()
            )
    </div>
</div>

<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>
public ActionResult Index()
{
    return View();
}

Populating items using data source

When rendering the Carousel component using data binding, you can assign a common template only for all items using the ItemTemplate property. You cannot set the interval for each item. The following example code depicts the functionality as data binding.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").DataSource((IEnumerable<object>)ViewBag.dataSource).ItemTemplate("<div class='slide-content'>${Title}</div>").Render())
    </div>
</div>

<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>
public IActionResult Index()
        {
            List<CarouselDataBinding> datasrc = new List<CarouselDataBinding>();
            datasrc.Add(new CarouselDataBinding
            {
                Id = 1,
                Title = "Slide 1",
            });
            datasrc.Add(new CarouselDataBinding
            {
                Id = 2,
                Title = "Slide 2",
            });
            datasrc.Add(new CarouselDataBinding
            {
                Id = 3,
                Title = "Slide 3",
            });
            datasrc.Add(new CarouselDataBinding
            {
                Id = 4,
                Title = "Slide 4",
            });
            datasrc.Add(new CarouselDataBinding
            {
                Id = 5,
                Title = "Slide 5",
            });
            ViewBag.dataSource = datasrc;
            return View();
        }
public class CarouselDataBinding
{
    public int Id { get; set; }
    public string Title { get; set; }
}

Selection

The Carousel items will be populated from the first index of the Carousel items and can be customized using the following ways,

  • Select an item using the property.
  • Select an item using the method.

Select an item using the property

Using the SelectedIndex property of the Carousel component, you can set the slide to be populated at the time of initial rendering else you can switch to the particular slide item.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").SelectedIndex(2).Items(new List<CarouselItem> {
                    new CarouselItem { Template = "<div class='slide-content'>Slide 1</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 2</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 3</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 4</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 5</div>"  }
                })
                .Render()
            )
    </div>
</div>

<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>
public ActionResult Index()
{
    return View();
}

Carousel selected slide

Select an item using the method

Using the prev or next public method of the Carousel component, you can switch the current populating slide to a previous or next slide.

@using Syncfusion.EJ2.Buttons;
@using Syncfusion.EJ2.Navigations;

@(Html.EJS().Button("prev").Content("Prev").Render())
@(Html.EJS().Button("next").Content("Next").Render())
<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").Items(new List<CarouselItem> {
                    new CarouselItem { Template = "<div class='slide-content'>Slide 1</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 2</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 3</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 4</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 5</div>"  }
                })
                .Render()
            )
    </div>
</div>

<script>
    document.getElementById('prev').onclick = function () {
        var carouselObj = document.querySelector(".e-carousel").ej2_instances[0];
        carouselObj.prev();
    }
    document.getElementById('next').onclick = function () {
        var carouselObj = document.querySelector(".e-carousel").ej2_instances[0];
        carouselObj.next();
    }
</script>

<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>
public ActionResult Index()
{
    return View();
}

Partial visible slides

The Carousel component supports to show one complete slide and a partial view of adjacent (previous and next) slides at the same time. You can enable or disable the partial slides using the partialVisible property.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").PartialVisible(true).Items(new List<CarouselItem> {
            new CarouselItem { Template = "#itemTemplate1"  },
            new CarouselItem { Template = "#itemTemplate2"  },
            new CarouselItem { Template = "#itemTemplate3"  },
            new CarouselItem { Template = "#itemTemplate4"  },
            new CarouselItem { Template = "#itemTemplate5"  }
            })
            .Render()
        )
    </div>
</div>
<script id="itemTemplate1" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/bridge.jpg" alt="bridge" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Golden Gate Bridge, San Francisco</figcaption>
    </figure>
</script>
<script id="itemTemplate2" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/trees.jpg" alt="spring_trees" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Spring Flower Trees</figcaption>
    </figure>
</script>
<script id="itemTemplate3" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/waterfall.jpg" alt="waterfall" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Oddadalen Waterfalls, Norway</figcaption>
    </figure>
</script>
<script id="itemTemplate4" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/sea.jpg" alt="sea" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Anse Source d'Argent, Seychelles</figcaption>
    </figure>
</script>
<script id="itemTemplate5" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/rocks.jpeg" alt="rocks" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Stonehenge, England</figcaption>
    </figure>
</script>
<style>
    .control-container {
        background-color: #e5e5e5;
        height: 360px;
        margin: 0 auto;
        width: 600px;
    }
  
    .img-container {
        height: 100%;
        margin: 0;
    }
  
    .img-caption {
        color: #fff;
        font-size: 1rem;
        position: absolute;
        bottom: 3rem;
        width: 100%;
        text-align: center;
    }
</style>
public ActionResult Index()
{
    return View();
}

NOTE

Slide animation only applicable if the partialVisible is enabled.

The last slide will be displayed as a partial slide at the initial rendering when the loop and partialVisible properties are enabled.

The previous slide is not displayed at the initial rendering when the loop is disabled.

The following example code depicts the functionality of partialVisible and without loop functionalities.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").PartialVisible(true).Loop(false).Items(new List<CarouselItem> {
            new CarouselItem { Template = "#itemTemplate1"  },
            new CarouselItem { Template = "#itemTemplate2"  },
            new CarouselItem { Template = "#itemTemplate3"  },
            new CarouselItem { Template = "#itemTemplate4"  },
            new CarouselItem { Template = "#itemTemplate5"  }
            })
            .Render()
        )
    </div>
</div>
<script id="itemTemplate1" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/bridge.jpg" alt="bridge" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Golden Gate Bridge, San Francisco</figcaption>
    </figure>
</script>
<script id="itemTemplate2" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/trees.jpg" alt="spring_trees" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Spring Flower Trees</figcaption>
    </figure>
</script>
<script id="itemTemplate3" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/waterfall.jpg" alt="waterfall" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Oddadalen Waterfalls, Norway</figcaption>
    </figure>
</script>
<script id="itemTemplate4" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/sea.jpg" alt="sea" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Anse Source d'Argent, Seychelles</figcaption>
    </figure>
</script>
<script id="itemTemplate5" type="text/x-template">
    <figure class="img-container">
        <img src="https://ej2.syncfusion.com/aspnetmvc/Content/carousel/images/rocks.jpeg" alt="rocks" style="height:100%; width: 100%;" />
        <figcaption class="img-caption">Stonehenge, England</figcaption>
    </figure>
</script>
<style>
    .control-container {
        background-color: #e5e5e5;
        height: 360px;
        margin: 0 auto;
        width: 600px;
    }
  
    .img-container {
        height: 100%;
        margin: 0;
    }
  
    .img-caption {
        color: #fff;
        font-size: 1rem;
        position: absolute;
        bottom: 3rem;
        width: 100%;
        text-align: center;
    }
</style>
public ActionResult Index()
{
    return View();
}

See also