62,046
社区成员
发帖
与我相关
我的任务
分享
@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();}}
public ViewResult Index(int id = 1)
{
using (var db = new MovieDbContext())
{
return View(db.Movies.OrderByDescending(a => a.Date).ToPagedList(id, 8));
}
}
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; }
}
}
@model PagedList<Movie>
<div id="movies">
@Html.Partial("_MoviesList",Model)
</div>
@section Scripts{@{Html.RegisterMvcPagerScriptResource();}}
@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"})
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);
}
}
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; }
}
}