Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am new to asp.net mvc razor.
I have a search form with multiple serach creteria (text box and dropdownlist).

My bug is that when i filter for exemple my table that initialy contains 100 rows, it gives me 50 rows. But when i click on second page (number 2 in the paging) i loose the filtering and the sélections in my 3 dropdownlists are null ( i loose the selection in my 3 dropdownlist, so all all parameters become null.)... so it gives me again 100 rows.

I tried this article, but i didn't understand .

Sample for to list, sort, search data and add pagination in ASP.Net MVC 5[^]



Please help. Thank you.

--========================================================================
-- Controller
--========================================================================
C#
public ActionResult Index(string sortOrder, string currentFilter, int? page, string EtablissementCode, string ClarderSousSecteur, string SearchString, int AnneeUniversitaire = 20122013)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.ClarderSousSecteurSortParm = String.IsNullOrEmpty(sortOrder) ? "ClarderSousSecteur_desc" : "ClarderSousSecteur_asc";

            if (SearchString != null)
            {
                page = 1;
            }
            else
            {
                SearchString = currentFilter;
            }
            ViewBag.CurrentFilter = SearchString;

            ViewBag.AnneeUniversitaire = new SelectList(db.tblAnneeDeclarations, "AnneeDeclarationCode", "AnneeDeclarationCode", AnneeUniversitaire);

            ViewBag.EtablissementCode = new SelectList(db.tblEtablissements, "EtablissementCode", "EtablissementCode", EtablissementCode);

            ViewBag.ClarderSousSecteur = new SelectList(db.tblClarderSousSecteur, "ClarderSousSecteurCode", "ClarderSousSecteurLibelle", ClarderSousSecteur);

            // Liste principale
            var USUAs = from m in db.tblUSUAs
                        join c in db.tblClarderSousSecteur on m.ClarderSousSecteurID equals c.ClarderSousSecteurID
                        select new USUAModel { USUAID = m.USUAID, USUACode = m.USUACode, USUALibelle = m.USUALibelle, AnneeUniversitaire = m.AnneeUniversitaire, EtablissementCode = m.EtablissementCode, ClarderSousSecteurID = c.ClarderSousSecteurID, ClarderSousSecteurCode = c.ClarderSousSecteurCode, ClarderSousSecteurLibelle = c.ClarderSousSecteurLibelle };

            if (!String.IsNullOrEmpty(SearchString))
            {
                USUAs = USUAs.Where(s => s.USUALibelle.ToUpper().Contains(SearchString.ToUpper())); //s => s.USUALibelle.Contains(SearchString));
            }

            if (!String.IsNullOrEmpty(AnneeUniversitaire.ToString()))
            {
                USUAs = USUAs.Where(y => y.AnneeUniversitaire == AnneeUniversitaire);
            }

            if (!String.IsNullOrEmpty(EtablissementCode))
            {
                USUAs = USUAs.Where(x => x.EtablissementCode == EtablissementCode);
            }

            if (!String.IsNullOrEmpty(ClarderSousSecteur))
            {
                USUAs = USUAs.Where(v => v.ClarderSousSecteurCode == ClarderSousSecteur);
            }

            switch (sortOrder)
            {
                case "ClarderSousSecteur_desc":
                    USUAs = USUAs.OrderByDescending(s => s.ClarderSousSecteurID);
                    break;
                case "ClarderSousSecteur_asc":
                    USUAs = USUAs.OrderBy(s => s.ClarderSousSecteurID);
                    break;
                default:
                    USUAs = USUAs.OrderBy(s => s.ClarderSousSecteurID);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(USUAs.ToPagedList(pageNumber, pageSize));
        }

--========================================================================
-- View
--========================================================================
HTML
@model PagedList.IPagedList<mvcapptablesreference.models.usuamodel>
@using PagedList.Mvc; 
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "USUAs destination";
}

<h2>Index</h2>
Votre recherche a retourné : @Model.Count() enregistrements

<p>
    @Html.ActionLink("Créer une nouvelle USUA", "Create")

    @using (Html.BeginForm("Index", "tblUSUAs", FormMethod.Get))
    {
    <p>
        Année universitaire : @Html.DropDownList("AnneeUniversitaire", (SelectList)ViewBag.AnneeUniversitaire)
           Etablissement : @Html.DropDownList("EtablissementCode", (SelectList)ViewBag.EtablissementCode)
           Clarder sous secteur : @Html.DropDownList("ClarderSousSecteur", (SelectList)ViewBag.ClarderSousSecteur)
    </p>
    <p>
        USUA : @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Filter" />
    </p>
    }

</p>

<table class="table">
    <tr>
        <th></th>
        <th>
            Code
        </th>
        <th>
            Libelle
        </th>
        <th>
            Etab.
        </th>
        <th>
            Année
        </th>
        <th>
@Html.ActionLink("S-Secteur", "Index", new { sortOrder = ViewBag.ClarderSousSecteurSortParm })
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.ActionLink("Editer", "Edit", new { id=item.USUAID }) |
            @Html.ActionLink("Details", "Details", new { id=item.USUAID }) |
            @Html.ActionLink("Supprimer", "Delete", new { id=item.USUAID })
        </td>
        <td>
			@item.USUACode
        </td>
        <td>
			@item.USUALibelle
        </td>
        <td>
			@item.EtablissementCode
        </td>
        <td>
			@item.AnneeUniversitaire
        </td>
        <td>
            @item.ClarderSousSecteurLibelle
        </td>
    </tr>
}

</table>

<br /> Page @(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}))</mvcapptablesreference.models.usuamodel>
Posted
Updated 14-Dec-18 2:23am
v2
Comments
Afzaal Ahmad Zeeshan 20-Aug-15 9:07am    
Please properly format your code.
Anil Shrestha 11-Apr-19 3:18am    
Good one.

1 solution

Hi,
It looks like you forgot to pass the other filters to the razor view in the paging section. The only search filter you have in the paging link is:
ViewBag.CurrentFilter


You need to include the selected values from the drop-down in the ViewBag collection and pass them to the view the same way you did for the "CurrentFilter" variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900