Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • namhoang1594 4 posts 74 karma points
    1 week ago
    namhoang1594
    0

    Attempted to divide by zero. Help newbie please!

    Hi, I am currently learning about Umbraco and have made a small demo. I want to make a photo album, but now I'm getting an error like this and I don't know how to fix it? please help me.

    And this is my code:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Home>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        var album = (Album)Model.Children.FirstOrDefault(x => x.ContentType.Alias.Equals("album"));
        var albumItems = album?.Children.ToList();
        int slideralbum = album.SliderShowForAlbum;
    }
    
    <section id="album" class="TN-Gallery wow fadeIn work-3col work-with-title gutter  no-padding-bottom">
        <div class="container">
            <div class="row">
                <h3 class="col-md-12 col-sm-12 text-center section-title">
                    <span class="display-block  title-bottom">@Umbraco.GetDictionaryValue("Album")</span>
                </h3>
            </div>
        </div>
        <div class="container">
            <div class="row margin-nine no-margin-lr no-margin-bottom">
                <div class="col-md-12 center-col margin-lr-auto text-center margin-seven no-margin-lr no-margin-top sm-padding-nine sm-no-margin-lr sm-no-padding-top xs-padding-two xs-no-margin-lr xs-no-padding-top">
                    <ul class="portfolio-filter portfolio-filter-tab-style-3 letter-spacing-1 xs-margin-eleven xs-no-margin-lr">
    
                    @{ if (albumItems != null && albumItems.Any())
                        {
                            foreach (AlbumItems item in albumItems)
                            {
                                <li class="xs-display-inline"><span>@item.AlbumItemsTitle </span></li>
                            }
                        } }
                </ul>
            </div>
        </div>
        <div class="d-none d-sm-none d-md-block">
            <div class="TN-list-gallery">
                @{ if (albumItems != null && albumItems.Any())
                    {
                        foreach (AlbumItems item in albumItems)
                        {
                            var sumAlbumImages = item.AlbumItemsImage.ToList();
                            var sumGridAlbum = Math.Ceiling(Convert.ToDecimal(sumAlbumImages.Count) / slideralbum);
                            var sumAlbumUrl = sumAlbumImages.Select(x => x.LocalCrops).ToList();
                            var albumJson = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(sumAlbumUrl);
                            int index = 0;
                            <div class="TN-grid-gallery">
    
                                @for (int i = 1; i <= sumGridAlbum; i++)
                                {
                                    int numberOfImages = 0;
                                    numberOfImages = slideralbum;
                                    if (numberOfImages >= sumAlbumImages.Count())
                                    {
                                        numberOfImages = sumAlbumImages.Count();
                                    }
                                    var pagedAlbumItems = sumAlbumImages.Skip((i - 1) * slideralbum).Take(numberOfImages).ToList();
                                    <div class="gallery-item">
                                        <ul class="gallery-item-list">
                                            @{ int order = 1; }
                                            @foreach (var urlAlbum in pagedAlbumItems)
                                            {
                                                string cropImages = "album";
                                                if (order % 2 == 0)
                                                {
                                                    cropImages = "albumImages";
                                                }
                                                <li class="liveshow TN-gallery-item [email protected](index)">
                                                    <span class="lazyloading gallery-img work-details-popup" data-list-image="@(albumJson)" data-idx="@index">
                                                        <img class="lazy" src="/images/lazy-load.png" data-src="@(urlAlbum?.GetLocalCropUrl(cropImages))" alt="" />
                                                    </span>
                                                </li>
                                                order++;
                                                index++;
                                            }
                                        </ul>
                                    </div>}
                            </div> }
                    } }
            </div>
        </div>
    

    I'm using Umbraco v8.14. Please, help me!!

  • Dan Diplo 1524 posts 6000 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    2

    You get this error if you attempt to divide a number by zero, since this is mathematically impossible (since there is an infinite amount of zeros in any given number).

    In your case, this would happen any time the variable slideralbum is zero. So you just need to check this isn't the case before you evaluate the following line:

    var sumGridAlbum = Math.Ceiling(Convert.ToDecimal(sumAlbumImages.Count) / slideralbum);

    So, make sure slideralbum is never zero.

  • namhoang1594 4 posts 74 karma points
    1 week ago
    namhoang1594
    0

    Thank you so much Dan. I fixed it!!

Please Sign in or register to post replies

Write your reply to:

Draft