大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
public class Movie
{
//主键
public int Id { get; set; }
//电影名称
public string MovieName { get; set; }
//票价
public decimal TicketPrice { get; set; }
//演播厅
public string ShowAddress { get; set; }
//上线日期
public DateTime ShowTime { get; set; }
//领衔主演
public string Starring { get; set; }
}
public ActionResult Index()
{
List<Movie> movies = new List<Movie>()
{
new Movie(){ Id=1, MovieName="晚秋", Starring="汤唯", ShowAddress="苏州科文中心", ShowTime=DateTime.Parse("2014-4-10 15:00:00"), TicketPrice=25.5M},
new Movie(){ Id=2, MovieName="神偷奶爸", Starring="XXX1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-1 14:30:00"), TicketPrice=20.5M},
new Movie(){ Id=3, MovieName="Yes Man", Starring="XXX2", ShowAddress="苏州印象城", ShowTime=DateTime.Parse("2014-5-2 14:25:00"), TicketPrice=21.5M},
new Movie(){ Id=4, MovieName="里约大冒险", Starring="未知", ShowAddress="苏州石路", ShowTime=DateTime.Parse("2014-5-3 14:40:00"), TicketPrice=22.5M},
new Movie(){ Id=5, MovieName="疯狂原始人", Starring="未知1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-4 14:00:00"), TicketPrice=23.5M},
new Movie(){ Id=6, MovieName="活着", Starring="葛优、巩俐", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-5 14:00:00"), TicketPrice=24.5M},
new Movie(){ Id=7, MovieName="北京遇上西雅图", Starring="汤唯", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-6 14:00:00"), TicketPrice=26.5M},
new Movie(){ Id=8, MovieName="人在囧途", Starring="徐峥", ShowAddress="XXX街影城", ShowTime=DateTime.Parse("2014-5-7 14:00:00"), TicketPrice=27.5M},
new Movie(){ Id=9, MovieName="神探狄仁杰", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-8 14:00:00"), TicketPrice=28.5M},
new Movie(){ Id=10, MovieName="里约大冒险2", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-9 14:00:00"), TicketPrice=29.5M},
};
return View(movies);
}
body {
}
table {
border: solid 1px #e8eef4;
border-collapse: collapse;
}
table td {
padding: 5px;
border: solid 1px #e8eef4;
width:100px;
text-align:center ;
background-color:orange;
}
table th {
padding: 6px 5px;
text-align:center;
background-color:#CC99CC;
border: solid 1px #e8eef4;
}
.rowStyle
{
background-color: #E8E8E8;
color: #000;
}
.gridhead {
background-color:yellow;
font-weight:bold;
text-align:center;
}
按照原先的简单做法,修改视图文件代码为:
@model IEnumerable<MvcWebGrid.Models.Movie>
@{
ViewBag.Title = "我的WebGrid";
}
<h2>我的WebGrid示例</h2>
@{
var grid = new WebGrid(
source: Model,
rowsPerPage: 4
);
}
@grid.GetHtml(
tableStyle: "table",
headerStyle: "gridhead",
mode:WebGridPagerModes.All,
firstText:"首页",
lastText:"尾页",
previousText:"上一页",
nextText:"下一页",
columns:
grid.Columns(
grid.Column("Id", "序号"),
grid.Column("MovieName", "电影名"),
grid.Column("Starring", "影院地址"),
grid.Column("TicketPrice", "票价"),
grid.Column("ShowAddress", "影院地址"),
grid.Column("ShowTime", "上映日期")
)
)
到此为止,是以前的做法,可以在页面进行分页,也可以排序,但是问题来了:
要看第n页,只要从数据库中查出第(n-1)*4+1~4*n条数据即可。
,其实很简单,不管用的是LINQ to Sql还是其它形式,无非就是
在前台查询指令,获得了WebGrid页和每页需要显示的行数的前提下,将指令转化成查询指定行的SQL
。
SELECT * FROM
(
SELECT ROW_NUMBER() OVER( ORDER BY 排序的字段 ) AS 序号,表.* FROM 表
) tmp
WHERE tmp.序号 between xxx to xxxx
现在的问题就是:我要在模型绑定时,知道WebGrid页—即用户点击了分页中的哪一页!这个十分简单,可以利用户点击下一页或上一页时,借助模型绑定,将webgrid页以参数形式传给action方法,即本例的Index方法,首先我得添加一个int型的参数:
public ActionResult Index(int page=1)
{
//...
}
其次,有了这个参数之后,后台就知道用户选了第几页,我后台就可以根据前面的算法,算出要查询第m~n行数据;
public class SelectMovies
{
//根据第n页查出的数据
public List<Movie> selectMovies { get; set; }
//数据总量
public int totalCount { get; set; }
}
2)修改Index方法为:
public ActionResult Index(int page=1)
{
const int totalCount = 10;
const int rowsPerPage = 4;
List<Movie> movies = new List<Movie>()
{
new Movie(){ Id=1, MovieName="晚秋", Starring="汤唯", ShowAddress="苏州科文中心", ShowTime=DateTime.Parse("2014-4-10 15:00:00"), TicketPrice=25.5M},
new Movie(){ Id=2, MovieName="神偷奶爸", Starring="XXX1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-1 14:30:00"), TicketPrice=20.5M},
new Movie(){ Id=3, MovieName="Yes Man", Starring="XXX2", ShowAddress="苏州印象城", ShowTime=DateTime.Parse("2014-5-2 14:25:00"), TicketPrice=21.5M},
new Movie(){ Id=4, MovieName="里约大冒险", Starring="未知", ShowAddress="苏州石路", ShowTime=DateTime.Parse("2014-5-3 14:40:00"), TicketPrice=22.5M},
new Movie(){ Id=5, MovieName="疯狂原始人", Starring="未知1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-4 14:00:00"), TicketPrice=23.5M},
new Movie(){ Id=6, MovieName="活着", Starring="葛优、巩俐", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-5 14:00:00"), TicketPrice=24.5M},
new Movie(){ Id=7, MovieName="北京遇上西雅图", Starring="汤唯", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-6 14:00:00"), TicketPrice=26.5M},
new Movie(){ Id=8, MovieName="人在囧途", Starring="徐峥", ShowAddress="XXX街影城", ShowTime=DateTime.Parse("2014-5-7 14:00:00"), TicketPrice=27.5M},
new Movie(){ Id=9, MovieName="神探狄仁杰", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-8 14:00:00"), TicketPrice=28.5M},
new Movie(){ Id=10, MovieName="里约大冒险2", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-9 14:00:00"), TicketPrice=29.5M},
};
var result = (from ms in movies select ms).Skip((page - 1) * rowsPerPage).Take(rowsPerPage);
SelectMovies sm = new SelectMovies()
{
selectMovies = result.ToList(),
totalCount=totalCount
};
return View(sm);
}
3)修改Index视图:
@model MvcWebGrid.Models.SelectMovies
@{
ViewBag.Title = "我的WebGrid";
}
<h2>我的WebGrid示例</h2>
@{
var grid = new WebGrid(
source: null,
rowsPerPage: 4
);
grid.Bind(Model.selectMovies, rowCount: Model.totalCount, autoSortAndPage: false);
}
@grid.GetHtml(
tableStyle: "table",
headerStyle: "gridhead",
mode:WebGridPagerModes.All,
firstText:"首页",
lastText:"尾页",
previousText:"上一页",
nextText:"下一页",
columns:
grid.Columns(
grid.Column("Id", "序号"),
grid.Column("MovieName", "电影名"),
grid.Column("Starring", "影院地址"),
grid.Column("TicketPrice", "票价"),
grid.Column("ShowAddress", "影院地址"),
grid.Column("ShowTime", "上映日期")
)
)
public ActionResult Index(int page=1,string sort="Id",string sortDir="ASC")
{
const int totalCount = 10;
const int rowsPerPage = 4;
List<Movie> movies = new List<Movie>()
{
new Movie(){ Id=1, MovieName="晚秋", Starring="汤唯", ShowAddress="苏州科文中心", ShowTime=DateTime.Parse("2014-4-10 15:00:00"), TicketPrice=25.5M},
new Movie(){ Id=2, MovieName="神偷奶爸", Starring="XXX1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-1 14:30:00"), TicketPrice=20.5M},
new Movie(){ Id=3, MovieName="Yes Man", Starring="XXX2", ShowAddress="苏州印象城", ShowTime=DateTime.Parse("2014-5-2 14:25:00"), TicketPrice=21.5M},
new Movie(){ Id=4, MovieName="里约大冒险", Starring="未知", ShowAddress="苏州石路", ShowTime=DateTime.Parse("2014-5-3 14:40:00"), TicketPrice=22.5M},
new Movie(){ Id=5, MovieName="疯狂原始人", Starring="未知1", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-4 14:00:00"), TicketPrice=23.5M},
new Movie(){ Id=6, MovieName="活着", Starring="葛优、巩俐", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-5 14:00:00"), TicketPrice=24.5M},
new Movie(){ Id=7, MovieName="北京遇上西雅图", Starring="汤唯", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-6 14:00:00"), TicketPrice=26.5M},
new Movie(){ Id=8, MovieName="人在囧途", Starring="徐峥", ShowAddress="XXX街影城", ShowTime=DateTime.Parse("2014-5-7 14:00:00"), TicketPrice=27.5M},
new Movie(){ Id=9, MovieName="神探狄仁杰", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-8 14:00:00"), TicketPrice=28.5M},
new Movie(){ Id=10, MovieName="里约大冒险2", Starring="未知", ShowAddress="观前街影城", ShowTime=DateTime.Parse("2014-5-9 14:00:00"), TicketPrice=29.5M},
};
IEnumerable<Movie> result;
if (sortDir=="ASC")
{
result = (from ms in movies orderby GetMovieProperty(ms, sort) ascending select ms).Skip((page - 1) * rowsPerPage).Take(rowsPerPage);
}
else
{
result = (from ms in movies orderby GetMovieProperty(ms, sort) descending select ms).Skip((page - 1) * rowsPerPage).Take(rowsPerPage);
}
SelectMovies sm = new SelectMovies()
{
selectMovies = result.ToList(),
totalCount=totalCount
};
return View(sm);
}
public object GetMovieProperty(object obj, string attrName)
{
var property = obj.GetType().GetProperty(attrName);
object value = property.GetValue(obj, null);
return value;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185348.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...