分页时ASP.NET MVC松散serach过滤器 [英] ASP.NET MVC loose serach filter when paging

查看:56
本文介绍了分页时ASP.NET MVC松散serach过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是asp.net mvc razor的新手。

我有一个带有多个serach creteria的搜索表单(正文框和下拉列表。



我的错误是当我过滤我的表时,初始包含100行,它给了我50行。但是当我点击第二页(分页中的第2个)时,我放松了过滤,我的3个下拉列表中的sélections为空(我在3下拉列表中放松了选择,所以所有参数都变为空。)...所以它给了我100行。



我试过这篇文章,但我不明白。



示例用于列出,排序,搜索数据和在ASP.Net MVC 5中添加分页 [ ^ ]







请帮忙。谢谢。



- ============================= ===========================================

- 控制器

- ==================================== ====================================

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);

            // 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
--========================================================================

@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>

推荐答案



看起来您忘记将其他过滤器传递到分页部分的剃刀视图中。您在分页链接中唯一的搜索过滤器是:
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





您需要包含所选内容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.


这篇关于分页时ASP.NET MVC松散serach过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆