MVC4 MvcPager的问题

qq_343194731 2014-10-23 02:07:51
我按照作者网站上的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; }
}
}


后台可以取到数据


结果如图



在线等待答案,谢谢。
...全文
164 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_343194731 2014-10-23
  • 打赏
  • 举报
回复
引用 4 楼 D56233577 的回复:
你分页是8行/页,我看你截图才6行记录,这样是不会出来分页导航的。 先排除这个问题再来问。
非常感谢,就是这个问题。原来对数据数量还有要求。
D56233577 2014-10-23
  • 打赏
  • 举报
回复
你分页是8行/页,我看你截图才6行记录,这样是不会出来分页导航的。 先排除这个问题再来问。
qq_343194731 2014-10-23
  • 打赏
  • 举报
回复
引用 2 楼 moonwrite 的回复:
既然响应了 那应该有所输出
右击 查看页面原代码 看看有没有生成对应的html




没有生成对应的html
moonwrite 2014-10-23
  • 打赏
  • 举报
回复
既然响应了 那应该有所输出 右击 查看页面原代码 看看有没有生成对应的html
qq_343194731 2014-10-23
  • 打赏
  • 举报
回复
来人看看给点意见呗~

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧