zoukankan      html  css  js  c++  java
  • [转].NET MVC 分页以及增删查改

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

    1. 数据库操作,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[0].Value = currPage;
                    sps[1].Value = pageSize;
                    dt = sh.ExecuteProcWithReturn(procName, sps);
                }catch(Exception ex){
                    throw ex;
                }
                return dt;
            }
    
    
            //get the movie count
            public Int32 GetMovieCount()
            {
                Int32 count = 1;
                string procName = "sp_Movie_GetCount";
                try
                {
         
                   DataTable dt = sh.ExecuteProcWithReturn(procName, null);
                   if (null != dt){
                       count = Convert.ToInt32(dt.Rows[0]["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[0].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[0].Value = movie.Id;
                    sps[1].Value = movie.Title;
                    sps[2].Value = movie.ReleaseDate;
                    sps[3].Value = movie.Category;
                    sps[4].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[0].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[0].Value = movie.Id;
                    sps[1].Value = movie.Title;
                    sps[2].Value = movie.ReleaseDate;
                    sps[3].Value = movie.Category;
                    sps[4].Value = movie.Price;
                    
                    flag = sh.ExecuteProcWithoutReturn(callName, sps);
    
               }
                catch (Exception ex)
                {
                    throw ex;
                }
                return flag;
            }
    
        }
    
     
    
     
    
     
    
     
    
    2.  控制层 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=1)
             {
                 Page p = new Page();
                 p.pageSize = 3;
                 Int32 lastPage =(Int32)Math.Ceiling(movieBLL.GetMovieCount()/(double)p.pageSize);
                 p.lastPage = lastPage;
                 if(currPage<1){
                     currPage = 1;
                 }
                 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");
             }
         }
     }
    
    
     
    
     
    
    3. 前台页面
    
    1. 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="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="2"><input type="submit" value="Submit" />
         <input  type="button" onclick="back();" value="Calcel"/>&nbsp;<%=ViewData["msg"]%>
         </td>
         </tr>
         </table>
         </form>
         </div>
     </body>
     </html>
    
    
     
    
     
    
    
     }
    
    
     
    
    2. 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
             {
                  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=" 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>
    
    
     
    
    3. 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
             {
                  95px;
             }
         </style>
     </head>
     <body>
         <div>
         <a href="/Movie/Create">新建</a><br/>
         <table>
         <tr>
         <th>标题</th>
          <th>发型日期</th>
           <th>总类</th>
            <th>价格</th>
            <th colspan="2">操作</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="2">
            <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="4">
                     <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>
    
    
     
    
    4. routine 的配置
    
     
    
      routes.MapRoute(
                   "Movie", // 路由名称
                  "Movie/{action}/{currPage}/{movieId}", // 带有参数的 URL
                   new { controller = "Movie", action = "Index", currPage = 1, movieId = UrlParameter.Optional } // 参数默认值
              );
    
                routes.MapRoute(
                "Default", // 路由名称
               "{controller}/{action}/{currPage}/{movieId}", // 带有参数的 URL
                new { controller = "Movie", action = "Index", currPage = 1, movieId = UrlParameter.Optional } // 参数默认值
           );
  • 相关阅读:
    C++ 的那些坑 (Day 0)
    LeetCode Search a 2D Matrix II
    最大子序列和问题的解(共4种,层层推进)
    如何编译文件(gcc + nasm)
    os如何处理键盘的所有按键,显示or不显示,显示是如何显示
    汇编操作显存
    diy文件系统上创建文件的流程
    在diy的文件系统上创建文件的流程
    建立文件系统
    快速选择
  • 原文地址:https://www.cnblogs.com/freeliver54/p/4449432.html
Copyright © 2011-2022 走看看