我正在尝试为我的视图添加分页并编辑我的控制器来创建它,但是当我尝试包含PagedList模型时,我得到一个错误,因为我不能包含我的原始模型。这就是我现在的看法:
@model IEnumerable<metric_tool.Models.Metric>
@model PagedList.IPagedList<metric_tool.Models.Metric>
@using PagedList
@{
ViewBag.Title = "Metrics List";
}
<h2>Metrics</h2>
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "Metric", FormMethod.Get))
{
<p>
Category: @Html.DropDownList("metricCategory","All")
Client: @Html.DropDownList("metricClient","All")
Phase: @Html.DropDownList("metricPhase","All")
Hierarchy: @Html.DropDownList("metricHierarchy", "All")
Thresholds: @Html.DropDownList("metricThreshold", "All") <br />
</p>
<p>
Data Source Search: @Html.TextBox("searchDataSrc") <input type="submit" value="Search" />
Description Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) <input type="submit" value="Search" />
</p>
}
</p>
<table class="table">
<tr>
<th>
@Html.ActionLink("Metric","Index", new { sortOrder = ViewBag.MetricSort, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Category", "Index", new { sortOrder = ViewBag.CategorySort, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.DisplayNameFor(model => model.SelectClient)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectPhase)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectHierarchy)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectDataSource)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectThresholds)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
.....
</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 }))
我正在考虑使用动态模型,但却无法让它工作。
答案 0 :(得分:0)
我实际上是这样做的。我有PagedViewModel.cs:
comment
- reply
-- reply to reply
- reply
-- reply to reply
comment
-reply
-- reply to reply
-- reply to reply
我有PagedResult:
using System.Collections.Generic;
namespace App.Web.ViewModels
{
public class PagedViewModel<T>
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
public long ItemCount { get; set; }
public int PageCount { get; set; }
public bool HasPrevPage { get; set; }
public bool HasNextPage { get; set; }
public List<T> Items { get; set; }
public PagedViewModel()
{
Items = new List<T>();
}
}
}
我的服务基础类是:
using System;
using System.Collections.Generic;
namespace App.Core.Infrastructure.Common.Paging
{
public class PagedResult<T>
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
public long ItemCount { get; set; }
public int PageCount { get; set; }
public bool HasPrevPage { get; set; }
public bool HasNextPage { get; set; }
public List<T> Items { get; set; }
public PagedResult(int pageNumber, int pageSize, long totalCount, IEnumerable<T> source)
{
Items = new List<T>();
Items.AddRange(source);
PageNumber = pageNumber;
PageSize = pageSize;
ItemCount = totalCount;
PageCount = (int)Math.Ceiling(totalCount / (double)pageSize);
HasPrevPage = PageNumber > 1;
HasNextPage = PageNumber < PageCount;
}
}
}
样品服务:
using App.Core.Infrastructure.Common.Paging;
using App.Core.Models;
using System;
using System.Linq;
namespace App.Core.Infrastructure.Services
{
public abstract class BaseService
{
protected PagedResult<T> GetPagedResult<T>(IQueryable<T> repository, int pageNumber, int pageSize)
where T : BaseEntity
{
pageNumber = Math.Max(1, pageNumber);
var total = repository.Count();
var items = repository
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
return new PagedResult<T>(pageNumber, pageSize, total, items);
}
}
}
我使用Automapper将PagedResult转换为PagedViewModel:
public class PlanService : BaseService
{
public PagedResult<Plan> GetPlans(int pageNumber, int pageSize)
{
return GetPagedResult(_database.Plans.OrderBy(x => x.Id), pageNumber, pageSize);
}
}
在视图中,您只需指定 Mapper.CreateMap<Plan, PlanViewModel>()
.ForMember(x => x.PlanTypeValue, o => o.MapFrom(s => s.PlanType))
.ForMember(x => x.PlanTypeText, o => o.MapFrom(s => s.PlanType.ToString()));
Mapper.CreateMap<PagedResult<Plan>, PagedViewModel<PlanViewModel>>();
即可。听起来有点复杂。但它运作得很好。