MVC5学习笔记,这次练习一下MVC列表的排序、筛选、分页。参考官网地址:Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application。
这次我们从头开始,首先我们创建一个空的MVC应用程序,之后先做一下准备工作,我们在根目录下添加Content文件夹并在创建子文件夹Bootstrap/3.3.6用来存放样式文件,然后Views文件夹中创建Shared/_LayoutMovie.cshtml布局文件,代码如下:
<span style="font-size:18px;"><!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - 我的电影</title>
<link href="~/Content/Bootstrap/3.3.6/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
@RenderBody()
</div>
</body>
</html></span>
随后,添加模型—Movie,代码如下:
<span style="font-size:18px;">using System;
using System.ComponentModel.DataAnnotations;
namespace DDZ.MVC5Paging.Models
{
public class Movie
{
public int ID { get; set; }
[Display(Name = "电影名称")]
[Required]
[MinLength(3)]
[MaxLength(50)]
public string Title { get; set; }
[Display(Name = "发行日期")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
[Display(Name = "电影类型")]
[StringLength(50)]
public string Genre { get; set; }
[Display(Name = "电影售价")]
[Range(0, 100)]
public decimal Price { get; set; }
}
}</span>
然后,我们选中“包含视图的MVC控制器(使用Entity Framework)”,按照下图创建控制器:
在调试之前呢,首先修改一下默认路由和web.config,如下图:
<span style="font-size:18px;">public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",