本文转自:http://blog.csdn.net/sust2012/article/details/30761867

. 数据库操作,DAL 层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.DataCommon;
using System.Data;
using System.Data.SqlClient;
using System.Collections; namespace DAL.DAL.Movie
{
public class MovieDAL
{
private readonly SqlHelper sh = new SqlHelper(); //pager query movie list
public DataTable QueryMovie(int currPage,int pageSize) {
DataTable dt = null;
string procName = "sp_Movie_GetPagerList";
try {
SqlParameter[] sps = {
new SqlParameter("@currPage",SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int)
};
sps[].Value = currPage;
sps[].Value = pageSize;
dt = sh.ExecuteProcWithReturn(procName, sps);
}catch(Exception ex){
throw ex;
}
return dt;
} //get the movie count
public Int32 GetMovieCount()
{
Int32 count = ;
string procName = "sp_Movie_GetCount";
try
{ DataTable dt = sh.ExecuteProcWithReturn(procName, null);
if (null != dt){
count = Convert.ToInt32(dt.Rows[]["mCount"]);
}
}
catch (Exception ex)
{
throw ex;
}
return count;
} //get the movie detail
public DataTable GetMovie(string movieId)
{
DataTable dt = null;
string procName = "sp_Movie_GetModel";
try
{
SqlParameter[] sps = {
new SqlParameter("@in_Id",SqlDbType.VarChar)
};
sps[].Value = movieId; dt = sh.ExecuteProcWithReturn(procName, sps);
}
catch (Exception ex)
{
throw ex;
}
return dt;
} //upload movie
public bool UpdateMovie(Model.Movie movie) {
bool flag = false;
string callName = "sp_Movie_Update";
try {
SqlParameter[] sps = {
new SqlParameter("@in_Id",SqlDbType.VarChar),
new SqlParameter("@in_Title",SqlDbType.NVarChar),
new SqlParameter("@in_ReleaseDate",SqlDbType.Date),
new SqlParameter("@in_Category",SqlDbType.NVarChar),
new SqlParameter("@in_Price",SqlDbType.Money)
};
sps[].Value = movie.Id;
sps[].Value = movie.Title;
sps[].Value = movie.ReleaseDate;
sps[].Value = movie.Category;
sps[].Value = movie.Price; flag = sh.ExecuteProcWithoutReturn(callName, sps);
}catch(Exception ex){
throw ex;
}
return flag;
} //delete moive
public bool DeleteMovie(string movieId) {
bool flag = false;
string callName = "sp_Movie_Delete";
try {
SqlParameter[] sps = {
new SqlParameter("@in_Id",SqlDbType.VarChar)
};
sps[].Value = movieId;
flag = sh.ExecuteProcWithoutReturn(callName, sps); }catch(Exception ex){
throw ex;
}
return flag;
} //save movie
public bool SaveMovie(Model.Movie movie) {
bool flag = false;
string callName = "sp_Movie_Add";
try
{
SqlParameter[] sps = {
new SqlParameter("@in_Id",SqlDbType.VarChar),
new SqlParameter("@in_Title",SqlDbType.NVarChar),
new SqlParameter("@in_ReleaseDate",SqlDbType.Date),
new SqlParameter("@in_Category",SqlDbType.NVarChar),
new SqlParameter("@in_Price",SqlDbType.Money)
};
sps[].Value = movie.Id;
sps[].Value = movie.Title;
sps[].Value = movie.ReleaseDate;
sps[].Value = movie.Category;
sps[].Value = movie.Price; flag = sh.ExecuteProcWithoutReturn(callName, sps); }
catch (Exception ex)
{
throw ex;
}
return flag;
} } . 控制层 Controller using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.Movie.BLL;
using Model;
using Model.VO; namespace RelationPro.Controllers
{
public class MovieController : Controller
{
//
// GET: /Movie/ private readonly MovieBLL movieBLL = new MovieBLL(); public ActionResult Index(int currPage=)
{
Page p = new Page();
p.pageSize = ;
Int32 lastPage =(Int32)Math.Ceiling(movieBLL.GetMovieCount()/(double)p.pageSize);
p.lastPage = lastPage;
if(currPage<){
currPage = ;
}
if (currPage> lastPage)
{
currPage = lastPage;
}
p.currPage = currPage; List<Movie> movieList = movieBLL.QueryMovie(currPage, p.pageSize);
ViewData["movieList"] = movieList;
ViewData["page"] = p;
return View(movieList);
} public ActionResult Edit(string movieId)
{
//取需要编辑的信息
Model.Movie movie = movieBLL.GetMovie(movieId);
ViewData["movie"] = movie;
return View(movie);
} [HttpPost]
public ActionResult Edit(Model.Movie movie)
{
//更新
bool flag = movieBLL.UpdateMovie(movie);
if(flag){
return RedirectToAction("/Index");
}
ViewData["msg"] = "更新失败";
return View(movie);
} public ActionResult Delete(int currPage,string movieId)
{
//根据id删除Movie
bool flag = movieBLL.DeleteMovie(movieId);
Session["msg"] = ""; if (!flag)
{
Session["msg"] = "删除失败.";
} return RedirectToAction("/index/"+currPage);
} public ActionResult Create() {
return View();
} [HttpPost]
public ActionResult Create(Movie movie) {
//save the movie
bool flag = movieBLL.SaveMovie(movie);
if(flag){
return RedirectToAction("/Index/");
}
ViewData["msg"] = "创建失败.";
return View("Create");
}
}
} . 前台页面 . Create.aspx <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Create</title>
<script type="text/javascript">
function back() {
window.location.href = "/Movie/Index";
}
</script>
</head>
<body>
<div>
<form action="" method="post">
<table align="center" style="width:60%;">
<tr>
<td>Title:</td>
<td>
<input name="Id" value="<%=Guid.NewGuid().ToString() %>" type="hidden"/>
<input name="Title"/>
</td>
</tr>
<tr>
<td>ReleaseDate:</td>
<td><input name="ReleaseDate"/></td>
</tr>
<tr>
<td>Category:</td>
<td><input name="Category"/></td>
</tr>
<tr>
<td>Price:</td>
<td><input name="Price"/></td>
</tr>
<tr>
<td colspan=""><input type="submit" value="Submit" />
<input type="button" onclick="back();" value="Calcel"/>&nbsp;<%=ViewData["msg"]%>
</td>
</tr>
</table>
</form>
</div>
</body>
</html> } . Edit.aspx <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Model.Movie>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Edit</title>
<style type="text/css">
.style1
{
width: 167px;
}
</style>
<script type="text/javascript">
function back() {
window.location.href = "/Movie/Index";
}
</script>
</head>
<body>
<div> <form action="/Movie/Edit/-1" method="post">
<table style="width: 60%;">
<tr>
<td class="style1">
Title:
</td>
<td>
<input name="Id" type="hidden" value="<%=Model.Id %>" />
<input name="Title" value="<%=Model.Title %>" />
</td> </tr>
<tr>
<td class="style1">
ReleaseDate:
</td>
<td>
<input name="ReleaseDate" value="<%=Model.ReleaseDate %>" />
</td> </tr>
<tr>
<td class="style1">
Category:
</td>
<td>
<input name="Category" value="<%=Model.Category %>" />
</td> </tr> <tr>
<td class="style1">
Price:
</td>
<td> <input name="Price" value='<%= Model.Price %>' />
</td> </tr>
<tr>
<td><input type="submit" value="Update"/></td>
<td><input type="button" onclick="back()" value="Cancel" style="height: 21px"/>&nbsp;<%=ViewData["msg"] %></td>
</tr>
</table>
</form>
</div>
</body>
</html> . Index.aspx <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>电影列表</title>
<style type="text/css">
.style1
{
width: 95px;
}
</style>
</head>
<body>
<div>
<a href="/Movie/Create">新建</a><br/>
<table>
<tr>
<th>标题</th>
<th>发型日期</th>
<th>总类</th>
<th>价格</th>
<th colspan="">操作</th>
</tr> <% List<Movie> movieList = (List<Movie>)ViewData["movieList"]; if(null != movieList){
Model.VO.Page page = (Model.VO.Page)ViewData["page"];
foreach(Movie m in movieList){
%>
<tr>
<td><%=m.Title %></td>
<td><%=m.ReleaseDate.ToString("yyyy-MM-dd")%></td>
<td><%=m.Category %></td>
<td><%=m.Price.ToString("#0.00") %></td>
<td class="style1" colspan="">
<a href="/Movie/Edit/1/<%=m.Id %>">编辑</a>
<a href="/Movie/Delete/<%=page.currPage %>/<%=m.Id %>">删除 &nbsp<%=Session["msg"]%></a>
</td>
</tr>
<%
} %>
<% %>
<tr>
<td colspan="">
<a href="/Movie/Index/1">首页</a>
<a href="/Movie/Index/<%=page.currPage-1 %>">上一页</a>
<a href="/Movie/Index/<%=page.currPage+1 %>">下一页</a> <a href="/Movie/Index/<%=page.lastPage %>">尾页</a>
</td>
<td>当前<%=page.currPage %>&nbsp;:共页<%=page.lastPage %></td> </tr>
<%
} %> </table>
</div> </body>
</html> . routine 的配置 routes.MapRoute(
"Movie", // 路由名称
"Movie/{action}/{currPage}/{movieId}", // 带有参数的 URL
new { controller = "Movie", action = "Index", currPage = , movieId = UrlParameter.Optional } // 参数默认值
); routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{currPage}/{movieId}", // 带有参数的 URL
new { controller = "Movie", action = "Index", currPage = , movieId = UrlParameter.Optional } // 参数默认值
);

[转].NET MVC 分页以及增删查改的更多相关文章

  1. backbonejs mvc框架的增删查改实例

    一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...

  2. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  3. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  4. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  5. knockout+MVC+webapi+sqlserver完成增删查改

    快过年了,公司的事情较少,想着开始学习点新东西.这段时间一个项目用到了mvc和webapi,然后一直对knockout比较感兴趣,就想着用这个框架做一个小实例.数据库采用的是sqlserver.话不多 ...

  6. 在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  7. 在MVC中使用泛型仓储模式和依赖注入实现增删查改

    标签: 原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository ...

  8. VS 自动创建带增删查改的MVC网站

    VS 自动创建带增删查改的MVC网站 MVC.Net教程   废话放在前头,说一下这个文章的缘起某天某妹纸找我,说这个MVC的创建不太会,要记一下controllers.models.还有页面引用的东 ...

  9. MVC——再探MVC——增删查改

    MVC 是我大学学的比较弱的,甚至不懂原理.(那时候都在准备蓝桥杯 软件杯比赛.) 在重新学 肯定要学MVC 现在知道了为什么叫MVC了  MVC是怎么工作的 MVC 是一个设计模式 控制器(Cont ...

随机推荐

  1. C语言中的声明与定义的差别

    1.对于以下的声明语句 int a;        假设其位置出如今全部的函数体之外,那么它就被称为外部对象a的定义.这个语句说明了a是一个外部整型变量,同一时候为a分配了存储空间.由于外部对象a并没 ...

  2. STM32低功耗模式与烟雾报警器触发信号电路设计

    1.STM32的3种低功耗模式 STM32有3种低功耗模式,分别是睡眠模式.停机模式和待机模式. 2.STM32在不同模式下的电流消耗 a.工作模式  消耗电流在27mA至36mA之间. b.睡眠模式 ...

  3. Jmeter代理服务器录制请求

    1.文档前提说明 1)本文使用jmeter的版本为 apache-jmeter-2.13 及以上版本 2)java版本要求在 1.8.0 以上 注:jmeter版本一般和java相应的版本一起使用,如 ...

  4. HDU 2746 Cyclic Nacklace

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. shell脚本怎么调试

    shell是Linux系统上常用的一种脚本语言.一般从事web后台开发的从业者,都会用到shell,因此shell调试也是一项必备的技能.本文教你如何进行shell脚本调试. 工具/原料   Linu ...

  6. html5--6-16 CSS3中的文字与字体

    html5--6-16 CSS3中的文字与字体 中文字体包很大,少量字体的话可以有其它方法. 有字库-首页-全球第一中文web font(在线字体)服务平台.web font.webfont.在线字体 ...

  7. 通过xmanager连接Linux图形界面

    今天要在linux下安装数据库,用的是xmanager:之前自己也用过该工具在OUI下做过安装,还很顺利. 但是,今天连接后,运行命令,等了有5分钟,没有出现OUI界面. linux版本: [root ...

  8. mediaplayer state

    enum media_player_states { MEDIA_PLAYER_STATE_ERROR = 0, MEDIA_PLAYER_IDLE = 1 << 0, MEDIA_PLA ...

  9. 频繁GC会造成卡顿

    频繁GC会造成卡顿 https://www.cnblogs.com/qcloud1001/p/9525078.html 一款app除了要有令人惊叹的功能和令人发指交互之外,在性能上也应该追求丝滑的要求 ...

  10. Leetcode字符串专题

    Leetcode38. Count and Say 分析:根据题意,数列的下一项就是统计上一项中每个数字出现的次数,理解清楚题意就很简单了 class Solution { public: strin ...