locked
Error: index was out of range RRS feed

  • Question

  • User-471172601 posted

    Hello everyone!

    I am working on a MVC Web Application where users can createa account, login, post new announcements etc and i have the next problem:

    I have a page /ViewAds where i can see all announcements and a search bar. The problem is when i search something i get error: 'Index was out of range. Must be non-negative and less than the size of  the collection. Parameter name: index'.

    Here is the controller for ViewAds:

    public ActionResult ViewAds(string searchString, int? page)
            {
                ViewBag.Message = "Lista tuturor anunturilor";
                List<AdViewModel> ads = new List<AdViewModel>();
                List<AdViewModel> data = new List<AdViewModel>();
                
                if(!string.IsNullOrEmpty(searchString))
                {
                    data = SearchByFilters(searchString);
                }
                else
                {
                    data = LoadAd();
                }
    
                int pageSize = 3;
                int pageNumber = (page ?? 1);
    
                foreach (var row in data)
                {
                    List<string> imgData = LoadImageUrl(row.Id);
                    ads.Add(new AdViewModel
                    {
                        Id = row.Id,
                        Title = row.Title,
                        Description = row.Description,
                        PublishDate = row.PublishDate,
                        Location = row.Location,
                        Status = row.Status,
                        PhotoUrl = imgData
                    });
                }
    
                @ViewBag.nrRez = ads.Count();
                return View(ads.ToPagedList(pageNumber, pageSize));
            }

    Here is the view:

    @using (Html.BeginForm("ViewAds", "Ad", FormMethod.Get))
    
    {
        <p>
            Find by name: @Html.TextBox("SearchString")
            <input type="submit" value="Search" />
        </p>
    
    }
    
    
     @foreach (var item in Model)
            {
                <div class="col-lg-4 col-sm-6 mb-30">
                    <div class="product-card mx-auto mb-3">
                        <div class="product-card-body">
                            <h5 class="product-title">@Html.ActionLink(@Html.DisplayFor(modelItem => item.Title).ToString(), "ViewAdDetails", "Ad", new { id = @Html.DisplayFor(modelItem => item.Id) }, null)</h5>
                            @*<h5 class="product-title"><a href="@Url.Action("ViewAdDetails", "Ad", new { id = @Html.DisplayFor(modelItem => item.Id) }) ">@Html.DisplayFor(modelItem => item.Title)</a></h5>*@
                        </div>
                        <a class="product-thumb" href="@Url.Action("ViewAdDetails", "Ad", new { id = @Html.DisplayFor(modelItem => item.Id) })">
                            <img src="@Html.DisplayFor(modelItem => item.PhotoUrl[0])" alt="Product Thumbnail" class="cover-img">
                        </a>
                        <div style="text-align:center;">
                            <span class="product-price">
                                <i class="fas fa-map-marker-alt"></i>&nbsp;&nbsp;
                                @Html.DisplayFor(modelItem => item.Location)
    
                            </span>
                            <span class="product-meta"><i class="far fa-calendar-alt"></i>&nbsp;&nbsp;@Html.DisplayFor(modelItem => item.PublishDate)</span>
                        </div>
                        <div class="product-buttons-wrap">
                            <div class="product-buttons">
                                <div class="product-button"><a href="#"><i class="fa fa-heart"></i></a></div>
                                <div class="product-button"><a href="#"><i class="fas fa-share-alt-square"></i></a></div>
                            </div>
                        </div>
                    </div>
                </div>
            }

    If i type a text in searchbox and there are no announcements containig this text it will show me there are no announcements. But, if there will be some announcements containing that text i will get the error.

    I have tryed to use  @for (int i = 0; i < ViewBag.nrRez; i++) instead foreach, but i get the same error. But, if i write 'manually' in view, to show me Model[0].Property it works. 

    Does anybody have any idea why? I am currently working on that problem for a few hours...

    Saturday, May 29, 2021 4:00 PM

All replies

  • User-474980206 posted

    You should use the debugger, but I assume the error is with the  PhotoUrl  collection

          <img src="@Html.DisplayFor(modelItem => item.PhotoUrl[0])" alt="Product Thumbnail" class="cover-img">
    Saturday, May 29, 2021 7:14 PM
  • User287926715 posted

    Hi salcudean,

    Open the F12 developer tool, where is the specific error reported in the console?

    Best Regards,

    ChaoDeng

    Tuesday, June 1, 2021 9:31 AM