Actionresult search result does not return all results for View Pagedlist

Asked

Viewed 94 times

1

I noticed that on my application before adding to View the Paged List he received all items returned from Actionresult responsible for the research, then from the implementation of the Paged List I don’t get all the results from my controller.

In summary Actionresult gets all search results but View does not receive all search results to be more specific, does not receive values from var research of the clause x.CategoryEmpresa.Any(c => c.Categoria.Name.Contains(company)) of the survey below

Instruction of the Research below

var pesquisa = db.Empresa.Where(x => x.Cidade.Contains(cidade) &&
                                (x.Nome.Contains(empresa)
                                || x.CategoriaEmpresa.Any(c => c.Categoria.Nome.Contains(empresa))))
                                .ToList(); 

Actionresult

public ActionResult Resultado(string empresa, string cidade, int? pagina)
{
    if (string.IsNullOrEmpty(empresa) || string.IsNullOrEmpty(cidade))
    {
        return Redirect("Index");
    }

    int pageSize = 4;
    int pageNumber = (pagina ?? 1);

    ViewBag.cidade = new SelectList(db.Empresa.Select(a => a.Cidade).Distinct());
    ViewBag.empresaPesquisada = empresa;
    ViewBag.cidadePesquisada = cidade;

    var pesquisa = db.Empresa.Where(x => x.Cidade.Contains(cidade) &&
                        (x.Nome.Contains(empresa)
                        || x.CategoriaEmpresa.Any(c => c.Categoria.Nome.Contains(empresa))))
                        .ToList();

    return View(pesquisa.ToPagedList(pageNumber, pageSize));
}

View Result - Configured with Paged List

@using PagedList.Mvc;
@model PagedList.IPagedList<SigaLista.Areas.Admin.Models.Empresa>

@{
    ViewBag.Title = "Resultado da Pesquisa";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container ajuste-rodape">
    <div class="row">
        <div class="col-md-8">
            <h4>Sua pesquisa retornou @Model.Count @(Model.Count > 1 ?  "Empresas" : "Empresa") </h4>

            @foreach (var item in Model)
            {                
                var destaque = false;
                if (!string.IsNullOrEmpty(item.Logomarca))
                {
                    destaque = true;
                }                
                <div class="media">
                    <div class="media-left media-middle">

                        @if (destaque)
                        {
                            <img src="@Url.Content(item.Logomarca)" alt="Image" style="width: 120px; height: auto;" />
                        }
                        else
                        {
                            <div class="sem-imagem"></div>
                        }

                    </div>
                    <div class="media-body">
                        <h4 class="media-heading">@Html.ActionLink(item.Nome, "Detalhes", new { id = item.Id })</h4>
                        <p>@Html.DisplayFor(modelItem => item.Telefone)</p>
                        <p>@Html.DisplayFor(modelItem => item.Endereco), @Html.DisplayFor(modelItem => item.Numero)</p>
                        <p>@Html.DisplayFor(modelItem => item.Cidade) @Html.DisplayFor(modelItem => item.Estado) @Html.DisplayFor(modelItem => item.Cep)</p>
                    </div>
                </div>
            }
        </div>    
        @Html.Partial("_PartialSideBar")
    </div>

    @*@if(Model.PageCount > 4)*@@* O ERRO ESTA AQUI *@
    @if(Model.TotalItemCount > 4)@* AQUI FUNCIONA PERFEITAMENTE *@
    {
        @:Página @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de @Model.PageCount
        @Html.PagedListPager(Model, page => Url.Action("Resultado", new { empresa = ViewBag.empresaPesquisada, cidade = ViewBag.cidadePesquisada, pagina = page }))
    }

</div>
  • You’re doing it wrong. In place of Tolist it has to be Topagedlist

  • @Virgilionovic I did as you said, alias had already tested this and did not succeed, but I made the return is like this and did not work: Return View(db.Empresa.Where(x => x.Cidade.Contains(city) && (x.nome.Contains(company) || x.Descriptcao.Contains(company) || x.CategoryEmpresa.Any(c => c.Categoria.Name.Contains(company))).Topagedlist(pageNumber, pageSize); Did not work.

  • @Virgilionovic is giving the following error: System.Notsupportedexception: 'The method 'Skip' is only supported for Sorted input in LINQ to Entities. The method 'Orderby' must be called before the method 'Skip'.'

  • I don’t know what you’ve been doing, but you’re doing it wrong

  • Faltou Orderby!

  • @Virgilionovic found the error, in which I did not return all the results of my search, and it was my mistake in choosing one of the features of Pagedlist that makes not appear the pager in the View, then always worked the communication between my Actionresult and View and so I share the solution that is this, modify the item within the if conditional thus getting @if(Model.Pagecount > 4) to @if(Model.Totalitemcount > 4) and so it will work perfectly.

  • Put this as an answer!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.