The search works fine, but when I change the page, It doesn´t apply the current filter, just show me the items without the searchString. How can I persist the current filter through all my pages?

Here is the controller:

public PartialViewResult Search(string sortOrder, string currentFilter, string searchString, int? page)
    {
        using (var parkingContext = new ParkingContext())
        {
            var subzones = SearchSortAndPaginate(sortOrder, currentFilter, searchString, page, parkingContext);
            return PartialView("_Search", subzones.ToPagedList(PageNumber, PageSize));
        }
    }

    private IQueryable<Subzone> SearchSortAndPaginate(string sortOrder, string currentFilter, string searchString, int? page, ParkingContext parkingContext)
    {

        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        if (searchString != null)
            page = 1;
        else
        searchString = currentFilter;
        ViewBag.CurrentFilter = searchString;
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        var subzones = parkingContext.Subzones.Include("Zone").Include("Street").Include("SubzoneClass").Where(s => s.Street.ParkingId == ParkingId && s.SubzoneRemoved==null);
        if (!string.IsNullOrEmpty(searchString))
            subzones = subzones.Where(s => s.SubzoneName.Contains(searchString) && s.Street.ParkingId == ParkingId);

        switch (sortOrder)
        {
            case "name_desc":
                subzones = subzones.OrderByDescending(s => s.SubzoneName);
                break;
            default:
                subzones = subzones.OrderBy(s => s.SubzoneName);
                break;
        }
        PageNumber = page ?? 1;
        return subzones;
    }

and the View:

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


<hgroup class="title">
    <h1>@ViewBag.Title</h1>
</hgroup>
<div class="container-fluid">
            <div class="col-md-12 m-t-per-1">
            <div class="form-group">
                <label class="col-md-offset-3 col-md-2 control-label">Buscar:</label>
                <div class="col-md-3">
                    @Html.TextBox("SearchString", null, new {@class = "form-control"})
                </div>
                <div class="col-md-1">
         @*                    @Html.ListBox("CurrentFilter",new List<SelectListItem>
                    {
                    }, new {@class = "form-control"})*@
                </div>
            </div>
            </div>
    @Html.Partial("_Search");

</div>

@section Scripts {
    <script type="text/javascript">
        $("#success-alert").fadeTo(2000, 500).slideUp(500, function () {
            $("#success-alert").slideUp(500);
        });
        $(function () {
            $("#SearchString").on("change keyup", function () {
                $.ajax({
                    url: '/Parking/Subzone/Search?SearchString=' + $("#SearchString").val(),
                    success: function (result) {
                        $("#subzoneList").html(result);
                    }
                });
            });
        });
    </script>
}

PartialView:

<div id="subzoneList">

<div class="row h4">
        @*<div class="col-md-2 text-center control-label"></div>*@
        <div class="col-md-3 text-center control-label">Nombre</div>
        <div class="col-md-3 text-center control-label">Calle</div>
        <div class="col-md-2 text-center control-label">Número</div>
        <div class="col-md-1 text-center control-label">Capacidad</div>
        <div class="col-md-1 text-center control-label">Editar</div>
        <div class="col-md-1 text-center control-label">Borrar</div>
    </div>
    @foreach (var subzone in Model)
    {
        <div class="row">
            @*<div class="col-md-2 text-center"></div>*@
            <div class="col-md-3 control-label text-center">@subzone.SubzoneName</div>
            <div class="col-md-3 control-label">@subzone.Street.StreetName</div>
            <div class="col-md-2 control-label text-center">@subzone.SubzoneNumber</div>
            <div class="col-md-1 control-label text-center">@subzone.SubzoneCapacity</div>
            <div class="col-md-1 text-center">@Html.ActionLink(" ", "Edit", "Subzone", new {id = subzone.SubzoneId}, new {@class = "glyphicon glyphicon-pencil", @title = "Editar"})</div>
            <div class="col-md-1 text-center">@Html.ActionLink(" ", "Delete", "Subzone", new {id = subzone.SubzoneId}, new {@class = "glyphicon glyphicon-trash", @title = "Borrar"})</div>

        </div>
    }

    <br/>

    @Html.ActionLink("Nueva Subzona", "Create", "Subzone", new {@class = "btn btn-info"})


    <br/>

<div class="text-center">
  P&aacute;gina @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de @Model.PageCount
  @Html.PagedListPager(Model, page => Url.Action("Index", new {page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter}))
</div>
<div class="text-center">
@Html.ActionLink("Descargar subzonas", "ExportData", "Subzone", new {@class="btn btn-info no-spin"})
</div>
</div>

Related posts

Recent Viewed