I'm using the Owl Carrousel library in a webassembly project, the carousel works fine for me when the images are static:
<img class="rounded-1 img-fluid" src="Img/CP5.png" alt="" / >
The problem arises when I want the images to be dynamic and come from the database, the carousel is not shown and when it is shown it does not work as it should, all the styles and functionality are lost, could you help me with this please.
this is my class where do I store the images in the database:
public class Carrusel
{
public int CarruselId { get; set; }
public string Imagen { get; set; }
}
on my blazor page I have this:
@if (ListadeCarrusel != null)
{
<div class="owl-carousel owl-theme">
@foreach (var item in ListadeCarrusel)
{
<div style="height:300px; width:100%;">
<img class="rounded-1 img-fluid" src="@item.Imagen" alt="" />
</div>
}
</div>
}
@code{
protected override async Task OnInitializedAsync()
{
await CargarCarrusel();
}
public List<Carrusel> ListadeCarrusel { get; set; }
private async Task CargarCarrusel()
{
var responseHttp = await repositorio.Get<List<Carrusel>>($"api/Carrusel");
if (!responseHttp.Error)
{
ListadeCarrusel = responseHttp.Response;
}
}
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JS.InvokeVoidAsync("OwlCarousel");
firstRender = false;
StateHasChanged();
}
}
}
in js file i have this:
function OwlCarousel() {
$(".owl-carousel").owlCarousel({
loop: true,
items: 1,
nav: false,
autoplay: true,
dots: true,
autoHeight: true
});
};
I think that the div with the owl carousel class, being inside the if, does not take the css and functionalities that make it work well, but I am not sure, I have no idea what is happening