学习MVC框架,处理分页和删除分页转跳的问题
第一次写博客,文采不好请多见谅,这里主要是写一下,自己是如何处理分页问题,我想初学者也遇到过这个问题。
分页的情况下,编辑信息有返回和编辑2个按钮,操作后都是应该返回原分页界面,使用TempData把分页的参数传递过去,但是只传递一个,另外一个不传递参数,导致点击返回能返回正常的分页,而点击编辑的情况下,直接返回到首页。
解决方式:大概的介绍下TempData的使用。TempData保存在Session中,Controller每次请求的时候都会从Session中获取 TempData,然后清除Session。基于这样的事实,在每次请求结束后,TempData的生命周期也就结束了。使用form传递另外一个参数,大概的解决方式说了
好了下面上代码让大家看下
首先我们要在”资料列表“界面中获取数据,首先是获取pageSize当前页的数据条数,page所在的第几页,recountCount数据的总条数
public ActionResult FileInfoList(int page = )
{
string errorMsg = TempData["errorMsg"] as string;
if (!string.IsNullOrEmpty(errorMsg))
{
ModelState.AddModelError("ErrorMsg", errorMsg);
}
int pageSize = ;
int pageIndex = page;
int recountCount = ;
ViewBag.PageSize = pageSize;
ViewBag.PageIndex = pageIndex; List<UploadFileModel> list = new List<UploadFileModel>();
IUploadFileService file = LoadService<IUploadFileService>();
Criteria c = new Criteria();
try
{ c.AddOrderBy(UploadFileModel._FileId, OrderByDirection.Desc); list = file.GetPagedUploadFileModel(c, pageIndex, pageSize, out recountCount);
}
catch (System.Exception e)
{
ModelState.AddModelError("ErrorMsg", "列表加载失败");
}
this.TempData["pageSize"] = pageSize;
this.TempData["page"] = page;
this.TempData["recountCount"] = recountCount;
return View(list);
}
用this.TempData[]的形式,把数据传递到需要调用数据的(编辑资料信息)界面 传递给ViewData["page"] = this.TempData["page"];的方式
/// <summary>
/// 编辑资料信息界面
/// </summary>
/// <param name="fileId"></param>
/// <returns></returns>
public ActionResult EditFileInfo(int fileId)
{
ViewData["page"] = this.TempData["page"];
if (fileId == )
{
return RedirectToAction("FileInfoList");
}
IUploadFileService upload = LoadService<IUploadFileService>();
UploadFileModel model = new UploadFileModel();
try
{
model = upload.GetUploadFileModelById(fileId);
}
catch (Exception e)
{
ModelState.AddModelError("ErrorMsg", "信息显示失败");
} return View(model);
}
前端通过 @Html.Hidden("Page", ViewData["page"])表单传递的方式隐藏的把page参数在传递到编辑方法代码中,如下面所示
/// <summary>
/// 编辑资料的方法
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult EditFileInfo(UploadFileModel model)
{
string message = "";
model.FileLastTime = DateTime.Now;
//通过表单的形式获取传递的page
int page = Convert.ToInt32(Request.Form["Page"]);
if (ModelState.IsValid)
{
IUploadFileService upload = LoadService<IUploadFileService>();
try
{
upload.Update(model);
message = "编辑成功";
TempData["message"] = message;
if (page > )
{ return RedirectToAction("FileInfoList", "FileManage", new { page });
}
return RedirectToAction("FileInfoList", "FileManage");
}
catch (Exception e)
{
ModelState.AddModelError("ErrorMsg", "编辑失败");
}
}
return View(model);
}
使用int page = Convert.ToInt32(Request.Form["Page"]);的方式把前台表单传递的page获取,用return RedirectToAction("FileInfoList", "FileManage", new { page });的方式返回分页。返回按钮直接通过界面中 ViewData["page"] = this.TempData["page"];的方法获取page的值。代码如下
<input type="button" name="btnBack" style="border-style:none" class="grayBtn" value="返 回" onclick="window.location.href='@Url.Action("FileInfoList", new { page = ViewData["page"] })'" />
这样的方式解决了ViewData和TempData传值的问题。
在写下关于删除的,要考虑的情况是,当你所处分页中只有最后一条数据的情况下,点击删除我们需要返回的不是本页而是上一页,如果是最后一页,则不做变动。如下我们看下代码
/// <summary>
/// 删除单个资料
/// </summary>
/// <param name="fileId"></param>
/// <returns></returns>
public ActionResult DeleFileInfo(int fileId)
{
UploadFileModel model = new UploadFileModel();
IUploadFileService file = LoadService<IUploadFileService>(); int page = (int)this.TempData["page"];
int recountCount = (int)this.TempData["recountCount"];
int pageSize = (int)this.TempData["pageSize"]; try
{
model = GetUploadFileModelByFileId(fileId);
file.Delete(model);
}
catch (Exception e)
{
ModelState.AddModelError("ErrorMsg", "删除失败");
}
//判断要删除的分页内数据情况,如果是分页内最后一条数据则删除后转跳到page-1的页面,否则转跳到page的界面
if (page > )
{
//使用向上取整防止出现BUG
if (Math.Ceiling(Convert.ToDouble(recountCount % (page * pageSize))) == )
{
page = page - ;
return RedirectToAction("FileInfoList", "FileManage", new { page });
}
return RedirectToAction("FileInfoList", "FileManage", new { page });
}
return RedirectToAction("FileInfoList", "FileManage");
}
这里的3个局部变量也是从“资料列表”界面获取,这里的pageSize 是每页的最大数据条数。我们这里先判断是否是最后一页if (page > 0),如果是在进入判断用 recountCount 总数据条数/(page第几页*pageSize每页的最大数据条数)的余数,然后使用Math.Ceiling进行向上取整,防止pageSize 是每页的最大数据条数不是10的情况下出现的小数,来判断是否是最后一条数据,如果是所在页面-1,返回上一页,不是则返回本页
学习MVC框架,处理分页和删除分页转跳的问题的更多相关文章
- 学习MVC框架之一
一.MVC的概述 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑和数据显示分离的 ...
- 学习MVC框架的步骤
1.搭建环境 2.了解控制层和视图层的映射 3.控制层和视图层的传值 交互 4.异常处理 5.页面标签 6.文件上传 7.框架源代码
- ASP.NET 平台下的MVC框架
这段时间在学习MVC框架,希望自己的一点心得能够帮助正在学习的同仁. 在阅读一些大牛的博客的时候看到一句话,感觉特别好,“你应该尝试MVC,是因为最终你会学到一些东西,它可以使你成为更好的Web开发人 ...
- 自定义MVC框架之工具类-分页类的封装
以前写过一个MVC框架,封装的有点low,经过一段时间的沉淀,打算重新改造下,之前这篇文章封装过一个验证码类. 这次重新改造MVC有几个很大的收获 >全部代码都是用Ubuntu+Vim编写,以前 ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- SSM框架——实现分页和搜索分页
登录|注册 在路上 在路上,要懂得积累:在路上,要学会放下:我在路上!Stay hungry,Stay foolish. 目录视图 摘要视图 订阅 [公告]博客系统优化升级 ...
- 动态多条件查询分页以及排序(一)--MVC与Entity Framework版url分页版
一.前言 多条件查询分页以及排序 每个系统里都会有这个的代码 做好这块 可以大大提高开发效率 所以博主分享下自己的6个版本的 多条件查询分页以及排序 二.目前状况 不论是ado.net 还是EF ...
- MVC3+EF4.1学习系列(三)-----排序 刷选 以及分页
上篇文章 已经做出了基本的增删改查 但这远远不足以应付实际的项目 今天讲下实际项目中 肯定会有的 排序 刷选 以及分页. 重点想多写点分页的 毕竟这个是任何时候都要有的 而且 我会尽量把这个 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)
前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...
随机推荐
- ELK学习实验018:filebeat收集docker日志
Filebeat收集Docker日志 1 安装docker [root@node4 ~]# yum install -y yum-utils device-mapper-persistent-data ...
- 「1.0」一个人开发一个App,小程序从0到1,起航了
古有,秦.齐.楚.赵.魏.韩.燕七国争雄:今有,微信.QQ.百度.支付宝.钉钉.头条.抖音七台争霸.古有,白起.李牧.王翦.孙膑.庞涓.赵奢.廉颇驰骋疆场:今有程序员1,程序员2,程序员3…编写代码. ...
- git 查看修改账号密码
git config user.name 查看用户名 git config user.email 查看用户邮箱 修改用户名和邮箱的命令 git config --glo ...
- 改进Zhang Suen细化算法的C#实现
本文主要实现了改进Zhang Suen细化算法的C#实现,相关论文 :“牟少敏,杜海洋,苏平,查绪恒,陈光艺.一种改进的快速并行细化算法[J].微电子学与计算机,2013,(第1期)” .这篇论文中关 ...
- Dynamics 365 CRM 配置field service mobile
配置field service mobile其实微软是有官方文档的, 但是没有坑的微软产品不是好产品. 一些细节设置文中还是没有考虑到的. 所以这里带大家配置一下field service mobil ...
- object-c中的int NSInteger NSUInteger NSNumber辨析
object-c中的int NSInteger NSUInteger NSNumber辨析 #import <Foundation/Foundation.h> int main(int a ...
- java操作数组转list集合删除元素报错ConcurrentModificationException
public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>( ...
- Oracle数据库安装与卸载
一.下载俩个压缩包,同时选中解压到一个文件夹中 二.点击step.exe(win10可能弹出不满足环境要求,选择是就行了) 三.把接收更新勾掉不需要 四.选择创建和配置数据库 五.选择服务器类 六.选 ...
- springIOC源码接口分析(四):MessageSource
一 定义方法 MessageSource接口用于支持信息的国际化和包含参数的信息的替换 这个接口定义了三个方法: public interface MessageSource { /** * 解析co ...
- MySQL Router单点隐患通过Keepalived实现
目录 一.介绍 二.环境准备 三.安装步骤 3.1下载软件包,解压 3.2源码安装 3.3配置keepalived 3.4修改keepalived配置文件 3.5启动keepalived 3.6查看V ...