向包含部分视图和视图模型(Razor、MVC)的视图添加分页

Add paging to view containing partial view and view model (Razor, MVC)

我正在尝试将分页添加到我的剃刀视图中。控制函数包含一个视图模型,视图也有一个部分视图 render.It 在 Model.FilterServices 和 Model.CustomerProfile 和 DisplayFor 上给出错误。

控制器搜索功能:

    public ActionResult Search(FilterServicesViewModel sp , int? page)
        {

            var data = from profile in _context.Customers
                       join service in _context.CustomerServices
                       on profile.strUserID equals service.strUserID
                       where profile.CustomerTypeId == 1 
                       select profile;

            data = data.OrderBy(profile => profile.Name);

            int pageSize = 3;
            int pageNumber = (page ?? 1);


            var dataPaged = data.ToPagedList(pageNumber, pageSize);

  IEnumerable<SelectListItem> items = _context.ServicesOffered.Select(c => new SelectListItem
            {
                Value = c.lngServiceID.ToString(),
                Text = c.Name
            });

            var serviceProviderViewModel = new ServiceProvider
            {
                FilterServices = new FilterServicesViewModel(),
                CustomerServices = new CustomerServices()

            };
            serviceProviderViewModel.FilterServices.FindService = sp.FindService;
            serviceProviderViewModel.FilterServices.ServicesOffered = items;
            serviceProviderViewModel.CustomerProfiles = dataPaged;

            return View("SearchServiceProviders", serviceProviderViewModel);         
        }

Razor 查看页面:

@model PagedList.IPagedList<DatabaseMVCPractice.ViewModels.ServiceProvider>
@using PagedList.Mvc;
@using PagedList;


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

@{
    ViewBag.Title = "SearchServiceProviders";
}

<h2>Search Service Providers</h2>
@{
    Html.RenderPartial("_FilterView",Model.FilterServices);
}


<table class="table">
    <tr>
        <th></th>
    </tr>

 @foreach (var item in Model.CustomerProfiles)
    {
        <tr>
            <td>       
                <img src="@Url.Content("~/Images/Thumbs/"+item.ProfileImage)" alt="Image" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WhenDate)
            </td>
        </tr>
}

</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

查看模型Class:

  public class ServiceProvider
    {
        public CustomerProfile Customer { get; set; }

        public IEnumerable<CustomerProfile> CustomerProfiles { get; set; }
        public CustomerServices CustomerServices { get; set; }

        public FilterServicesViewModel FilterServices { get; set; }
    }

我收到一条错误消息:

IPagedList<ServiceProvider>' does not contain a definition for 'CustomerProfiles' and no extension method 'CustomerProfiles' accepting a first argument of type 'IPagedList<ServiceProvider>' could be found (are you missing a using directive or an assembly

它在 Model.FilterServices 和 Model.CustomerProfile 以及 DisplayFor

上出错

在您的视图模型中,您的模型属于分页列表类型。由于您有多个模型,您可以 运行 循环查找您需要的模型。

@{
    foreach (var p in Model)
     {
        Html.RenderPartial("_FilterView", p.FilterServices);
     }
 }

@Html.PagedListPager(Model, page => Url.Action("Search",
    new { page }))

此外,在您的控制器中,创建了一个静态分页列表并将其传递到您的视图中

 List <ServiceProvider> serviceViews = new List<ServiceProvider>();

 serviceViews.Add(serviceProviderViewModel);

 int totalProfiles = data.Count();

 IPagedList<ServiceProvider> serviceProviders = new StaticPagedList<ServiceProvider>(serviceViews, pageNumber, pageSize,totalProfiles);

 return View("SearchServiceProviders", serviceProviders);