MVC4 MvcPager的有关问题
MVC4 MvcPager的问题
我按照作者网站上的demo代码写的,编译通过了。运行时没有显示分页功能,URL路由分页和AJAX分页都试过了,全都没有显示,为什么?
URL路由分页代码
View
Controllers
Modle
AJAX分页代码
View
_MoviesList.cshtml
Controller
Model
后台可以取到数据

结果如图

在线等待答案,谢谢。
------解决思路----------------------
你分页是8行/页,我看你截图才6行记录,这样是不会出来分页导航的。
先排除这个问题再来问。
我按照作者网站上的demo代码写的,编译通过了。运行时没有显示分页功能,URL路由分页和AJAX分页都试过了,全都没有显示,为什么?
URL路由分页代码
View
@model PagedList<Movie>
@{
ViewBag.Title = "首页";
}
<h2>
目录</h2>
<p>
@Html.ActionLink("新建", "Create")
</p>
<table width="100%">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
@Html.ActionLink("详情", "Details", new { id = item.ID }) |
@Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
@Html.Pager(Model,new PagerOptions{PageIndexParameterName = "id",ShowPageIndexBox = true,PageIndexBoxType = PageIndexBoxType.DropDownList,ShowGoButton = false})
@section Scripts{@{Html.RegisterMvcPagerScriptResource();}}
Controllers
public ViewResult Index(int id = 1)
{
using (var db = new MovieDbContext())
{
return View(db.Movies.OrderByDescending(a => a.Date).ToPagedList(id, 8));
}
}
Modle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Webdiyer.MvcPagerDemo.Models
{
public class Movie
{
public int ID { get; set; }
[Display(Name = "片名")]
[Required]
public string Name { get; set; }
[Display(Name = "影片类型")]
[Required]
public string Genre { get; set; }
[Display(Name = "票价")]
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[Display(Name = "上映时间")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
[Display(Name = "影评")]
[StringLength(5)]
public string Rating { get; set; }
}
public class MovieDbContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
AJAX分页代码
View
@model PagedList<Movie>
<div id="movies">
@Html.Partial("_MoviesList",Model)
</div>
@section Scripts{@{Html.RegisterMvcPagerScriptResource();}}
_MoviesList.cshtml
@model PagedList<Movie>
<table width="100%">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
操作
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.ActionLink("修改", "Edit", new { id = item.ID }) |
@Html.ActionLink("详情", "Details", new { id = item.ID }) |
@Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
@Ajax.Pager(Model,new PagerOptions{PageIndexParameterName = "id",ShowPageIndexBox = true,PageIndexBoxType = PageIndexBoxType.DropDownList,ShowGoButton = false},new MvcAjaxOptions{UpdateTargetId = "movies"})
Controller
public ActionResult _MoviesList(int id = 1)
{
using (var db = new MovieDbContext())
{
var model = db.Movies.OrderByDescending(a => a.Date).ToPagedList(id, 8);
if (Request.IsAjaxRequest())
return PartialView("_MoviesList", model);
return View(model);
}
}
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Webdiyer.MvcPagerDemo.Models
{
public class Movie
{
public int ID { get; set; }
[Display(Name = "片名")]
[Required]
public string Name { get; set; }
[Display(Name = "影片类型")]
[Required]
public string Genre { get; set; }
[Display(Name = "票价")]
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[Display(Name = "上映时间")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
[Display(Name = "影评")]
[StringLength(5)]
public string Rating { get; set; }
}
public class MovieDbContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
后台可以取到数据
结果如图
在线等待答案,谢谢。
------解决思路----------------------
你分页是8行/页,我看你截图才6行记录,这样是不会出来分页导航的。
先排除这个问题再来问。