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

时间:2019-05-07 18:26:07

标签: asp.net razor model-view-controller pagedlist

我正在尝试将分页添加到我的剃刀视图中。控件功能包含视图模型,视图还具有部分视图要呈现。它在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);         
        }

剃刀查看页面:

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

查看模型类:

  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上显示错误

1 个答案:

答案 0 :(得分:1)

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

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