在ASP.NET MVC中使用PagedList之2--将查询关键字传入后端

1. View


@{
    ViewBag.Title = "Processes Listing";
}
@using System.Reflection
@using PagedList.Mvc;
@using PagedList;
@using PagedListTest.Controllers


<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />


<form id="frmSearch" method="GET" action="@Url.Action("Index")">
    <div>
        Process Name @Html.TextBox("processName")
    </div>
    <div>
        Machine Name @Html.TextBox("machineName")
    </div>


<input type="submit" value="Search"/>
</form>




<h2>List of Processes</h2>
<ul>
    @foreach (var process in ViewBag.Processes)
    {
        <li>@process.ProcessName</li>
    }
</ul>


@Html.PagedListPager((IPagedList)ViewBag.Processes,
page => Url.Action("Index", new { page }))




@section scripts{
    <script>
        $(".pagination > li > a").click(function () {
            var pageUrl = $(this).attr("href");
            var queryStr = $("#frmSearch").serialize();


            $(this).attr("href", pageUrl + "&" + queryStr);
        });
    </script>
}







2.C# 


 
public object Index(int? page, string processName, string machineName)
        {
            var processes = System.Diagnostics.Process.GetProcesses();
            if (!string.IsNullOrWhiteSpace(processName))
            {
                processes = processes.Where(p => p.ProcessName.Contains(processName)).ToArray();
            }
            if (!string.IsNullOrWhiteSpace(machineName))
            {
                processes = processes.Where(p => p.MachineName.Contains(machineName)).ToArray();
            }


            var pageNumber = page ?? 1;
            var onePageOfProcesses = processes.ToPagedList(pageNumber, 25);


            ViewBag.Processes = onePageOfProcesses;
            return View();
        }


你可能感兴趣的:(在ASP.NET MVC中使用PagedList之2--将查询关键字传入后端)