View:
@model PagedList
@Html.Partial("_ArticleTable", Model)
@Html.Pager(Model).Options(o => o.SetPagerItemTemplate("{0} "))
_ArticleTable.cshtml:
@model PagedList
序号 | @Html.DisplayNameFor(model => model.Title) | @Html.DisplayNameFor(model => model.PubDate) | @Html.DisplayNameFor(model => model.Author) | @Html.DisplayNameFor(model => model.Source) |
---|
@{ int i = 0;}
@foreach (var item in Model)
{
@(Model.StartItemIndex + i++)@Html.DisplayFor(modelItem => item.Title)
@Html.DisplayFor(modelItem => item.PubDate)
@Html.DisplayFor(modelItem => item.Author)
@Html.DisplayFor(modelItem => item.Source)
}
Model:
public class Article
{
[Display(Name="文章编号")]
public int ID { get; set; }
[Display(Name="文章标题")]
[MaxLength(200)]
public string Title { get; set; }
[Display(Name = "文章内容")]
public string Content { get; set; }
[Display(Name = "发布日期")]
public DateTime PubDate { get; set; }
[Display(Name = "作者")]
[MaxLength(20)]
public string Author { get; set; }
[Display(Name = "文章来源")]
[MaxLength(20)]
public string Source { get; set; }
}
Controller:
public ViewResult QueryString(int pageIndex = 1)
{
using (var db = new DataContext())
{
return View(db.Articles.OrderByDescending(a => a.PubDate).ToPagedList(pageIndex, 5));
}
}