ASP.NET CORE Razor页面中的分页和搜索问题

时间:2019-02-13 06:04:03

标签: asp.net-core asp.net-core-2.0 razor-pages

我想在来自https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.2的Razor Pages参考中的搜索和分页中显示表中的数据。 我正在运行项目并搜索结果ok.but我想分页样式并使用以下分页样式搜索数据列表: enter image description here

我的PaginatedList.cs是

namespace HluttawWebApp.Models
{

    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            TotalPages = (int)Math.Ceiling(count / (double)pageSize);

            this.AddRange(items);
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex < TotalPages);
            }
        }

        public static async Task<PaginatedList<T>> CreateAsync(
            IQueryable<T> source, int pageIndex, int pageSize)
        {
            var count = await source.CountAsync();
            var items = await source.Skip(
                (pageIndex - 1) * pageSize)
                .Take(pageSize).ToListAsync();


            return new PaginatedList<T>(items, count, pageIndex, pageSize);
        }
    }
}

我的Indext.chtml是

namespace HluttawWebApp.Pages.Position
{
    public class IndexModel : PageModel
    {
        private readonly HluttawWebApp.Data.ApplicationDbContext _context;

        public IndexModel(HluttawWebApp.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public PaginatedList<Positions> Positions { get; set; }
        public string CurrentFilter { get; set; }
        public async Task OnGetAsync(string currentFilter, string searchString, int? pageIndex)
        {

            if (searchString != null)
            {
                pageIndex = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            CurrentFilter = searchString;

            IQueryable<Positions> studentIQ = from s in _context.Positions
                                              select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                studentIQ = studentIQ.Where(s => s.PositionName.Contains(searchString)
                                       || s.Salary.Contains(searchString));
            }


            int pageSize = 3;
            Positions = await PaginatedList<Positions>.CreateAsync(
                studentIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
        }



    }
}

我的观点是

@page "{pageIndex:int?}"
@model HluttawWebApp.Pages.Position.IndexModel


@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<form asp-page="./Index" method="get">
    <div class="form-actions no-color">
        <p>
            Find by name: <input type="text" name="SearchString" value="" />
            <input type="submit" value="Search" class="btn btn-default" /> |
            <a asp-page="./Index">Back to full List</a>
        </p>
    </div>
</form>



<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Positions[0].PositionName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Positions[0].Salary)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Positions)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.PositionName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Salary)
                </td>
                <td>
                    <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>


@{
        var prevDisabled = !Model.Positions.HasPreviousPage ? "disabled" : "";
        var nextDisabled = !Model.Positions.HasNextPage ? "disabled" : "";
    }


<ul class="pagination">
    <li class="page-item">
        <a asp-page="./Index"
           asp-route-pageIndex="@(Model.Positions.PageIndex - 1)"
           asp-route-currentFilter="@Model.CurrentFilter"
           class="page-link btn btn-default @prevDisabled">
            Previous
        </a>
    </li>

    **This part is problems main point and help me**
@for (int i = 1; i <= Model.Positions.Count; i++)
    {
        <li class="page-item">
            <a class="page-link" asp-page="./Index" asp-route-pageIndex="@i"  asp-route-currentFilter="@Model.CurrentFilter">@i</a>
        </li>
    }

    <li class="page-item">
        <a asp-page="./Index"
           asp-route-pageIndex="@(Model.Positions.PageIndex + 1)"
           asp-route-currentFilter="@Model.CurrentFilter"
           class="page-link btn btn-default @nextDisabled">
            Next
        </a>
    </li>
</ul>

请帮助我,非常感谢您的回答。

0 个答案:

没有答案