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
--========================================================================
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);
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()));
}
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
--========================================================================
@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>