栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

MVC+jQuery.Ajax异步实现增删改查和分页

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVC+jQuery.Ajax异步实现增删改查和分页

本文实例为大家分享了MVC+jQuery.Ajax异步实现增删改查和分页的具体代码,供大家参考,具体内容如下

1、Model层代码

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MvcExamples;
using System.Web.Mvc;

namespace MvcExamples.Web.Models
{
 public class StudentModels
 {
 /// 
 /// 获取学生信息列表
 /// 
 public List StudentList { get; set; }
 /// 
 /// 获取教工信息列表
 /// 
 public List TeacherList { get; set; }
 /// 
 /// 获取学生信息列表(分页)
 /// 
 public PagedList GetStudentList { get; set; }
 }
}

2、View层代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


 Index



 
 
 
 
 



 
 MVC 演示
 
  <%foreach (MvcExamples.Model.Student student in Model.GetStudentList)
  {%>
  
  <% } %>
 
学生表
学号 姓名 性别 生日 班级 操作
<%=student.sno %> <%=student.sname %> <%=student.ssex %> <%=student.sbirthday %> <%=student.sclass %> " sname="<%=student.sname %>" ssex="<%=student.ssex %>" sbirthday="<%=student.sbirthday %>" sclass="<%=student.sclass %>">修改    ">删除
全选 添加
<%=Html.MikePager(Model.GetStudentList)%>
<%foreach (MvcExamples.Model.Student student in Model.StudentList) {%> <% } %>
学生表
学号 姓名 性别 生日 班级
<%=student.sno %> <%=student.sname %> <%=student.ssex %> <%=student.sbirthday %> <%=student.sclass %>

<%foreach (MvcExamples.Model.Teacher teacher in Model.TeacherList) {%> <% } %>
老师表
编号 姓名 性别 生日 职称 所在部门
<%=teacher.tno%> <%=teacher.tname%> <%=teacher.tsex%> <%=teacher.tbirthday%> <%=teacher.prof%> <%=teacher.depart%>
姓名:
性别:
生日:
班级:

3、Controller层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace MvcExamples.Web.Controllers
{
 public class StudentController : Controller
 {
 //
 // GET: /Student/

 MvcExamples.BLL.Student _Student = new MvcExamples.BLL.Student();
 MvcExamples.BLL.Teacher _Teacher = new MvcExamples.BLL.Teacher();
 /// 
 /// 演示
 /// 
 /// 
 /// 
 /// 
 public ActionResult Index(int? pi, string sclass)
 {
  int PageIndex = pi ?? 1;
  int PageSize = 5;
  string sClass = sclass == null ? "95031" : sclass;
  MvcExamples.Web.Models.StudentModels _StudentModels = new MvcExamples.Web.Models.StudentModels();
  _StudentModels.StudentList = _Student.GetModelList("sclass=" + sClass);
  _StudentModels.TeacherList = _Teacher.GetModelList("tsex='男'");
  _StudentModels.GetStudentList = new PagedList(_Student.GetModelList("sclass=" + sClass).AsQueryable(), PageIndex, PageSize);
  return View(_StudentModels);//返回一个Model
 }
 /// 
 /// 修改学生信息
 /// 
 /// 
 /// 
 /// 
 /// 
 /// 
 /// 
 public ActionResult UpdateStudent(string no, string name, string sex, string birsthday, string sclass)
 {
  MvcExamples.Model.Student _student = new MvcExamples.Model.Student();
  _student.sno = no;
  _student.sname = name;
  _student.ssex = sex;
  _student.sbirthday = Convert.ToDateTime(birsthday);
  _student.sclass = sclass;

  _Student.Update(_student);  

  JsonResult json = new JsonResult();
  json.Data = new
  {
  result = "true"
  };
  return json;
 }
 /// 
 /// 删除学生信息
 /// 
 /// 
 /// 
 public ActionResult DeleteStudent(string no)
 {
  bool IsDelete= _Student.Delete(no);
  JsonResult json = new JsonResult();
  return json;
  if (IsDelete)
  {
  json.Data = new
  {
   result = "true"
  };
  }
  else
  {
  json.Data = new
  {
   result ="false"
  };
  }
  return json;
 }
 /// 
 /// 添加学生信息
 /// 
 /// 
 /// 
 /// 
 /// 
 /// 
 /// 
 public ActionResult AddStudent(string no, string name, string sex, string birsthday, string sclass)
 {
  MvcExamples.Model.Student _student = new MvcExamples.Model.Student();
  _student.sno = no;
  _student.sname = name;
  _student.ssex = sex;
  _student.sbirthday = Convert.ToDateTime(birsthday);
  _student.sclass = sclass;

  _Student.Add(_student);

  JsonResult json = new JsonResult();
  json.Data = new
  {
  result = "true"
  };
  return json;
 }

 /// 
 /// 提供弹出窗口的数据
 /// 
 /// 
 /// 
 public ActionResult WindowData(int id)
 {
  JsonResult json = new JsonResult();
  //这里给json数据(这里只是演示,下面数据是模拟的)
  json.Data = new
  {
  name = "张三",
  sex = "男"
  };
  return json;
 }

 }
}

4、两个分页辅助类PagedList和MikePagerHtmlExtensions

PagedList辅助类

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace System.Web.Mvc
{
 public interface IPagedList
 {
 int TotalPage //总页数
 {
  get;
 }

 int TotalCount
 {
  get;
  set;
 }

 int PageIndex
 {
  get;
  set;
 }

 int PageSize
 {
  get;
  set;
 }

 bool IsPreviousPage
 {
  get;
 }

 bool IsNextPage
 {
  get;
 }
 }

 public class PagedList : List, IPagedList
 {
 public PagedList(IQueryable source, int? index, int? pageSize)
 {
  if (index == null) { index = 1; }
  if (pageSize == null) { pageSize = 10; }
  this.TotalCount = source.Count();
  this.PageSize = pageSize.Value;
  this.PageIndex = index.Value;
  this.AddRange(source.Skip((index.Value - 1) * pageSize.Value).Take(pageSize.Value));
 }

 public int TotalPage
 {
  get { return (int)System.Math.Ceiling((double)TotalCount / PageSize); }
 }

 public int TotalCount
 {
  get;
  set;
 }
 /// 
/// 
/// 
 public int PageIndex
 {
  get;
  set;
 }

 public int PageSize
 {
  get;
  set;
 }

 public bool IsPreviousPage
 {
  get
  {
  return (PageIndex > 1);
  }
 }

 public bool IsNextPage
 {
  get
  {
  return ((PageIndex) * PageSize) < TotalCount;
  }
 }

 }

 public static class Pagination
 {
 public static PagedList ToPagedList(this IOrderedQueryable source, int? index, int? pageSize)
 {
  return new PagedList(source, index, pageSize);
 }

 public static PagedList ToPagedList(this IOrderedQueryable source, int? index)
 {
  return new PagedList(source, index, 10);
 }

 public static PagedList ToPagedList(this IQueryable source, int? index, int? pageSize)
 {
  return new PagedList(source, index, pageSize);
 }

 public static PagedList ToPagedList(this IQueryable source, int? index)
 {
  return new PagedList(source, index, 10);
 }
 }
}

MikePagerHtmlExtensions辅助类

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;

namespace System.Web.Mvc
{
 public static class MikePagerHtmlExtensions
 {
 
 #region MikePager 分页控件

 public static string MikePager(this HtmlHelper html, PagedList data)
 {
  string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
  return MikePager(html, data, actioinName);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, object values)
 {
  string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
  return MikePager(html, data, actioinName, values);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, string action)
 {
  return MikePager(html, data, action, null);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, string action, object values)
 {
  string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
  return MikePager(html, data, action, controllerName, values);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, string action, string controller, object values)
 {
  return MikePager(html, data, action, controller, new RoutevalueDictionary(values));
 }

 public static string MikePager(this HtmlHelper html, PagedList data, RoutevalueDictionary values)
 {
  string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
  return MikePager(html, data, actioinName, values);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, string action, RoutevalueDictionary values)
 {
  string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
  return MikePager(html, data, action, controllerName, values);
 }

 public static string MikePager(this HtmlHelper html, PagedList data, string action, string controller, RoutevalueDictionary valuedic)
 {
  int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
  int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage;

  RoutevalueDictionary vs = valuedic == null ? new RoutevalueDictionary() : valuedic;

  var builder = new StringBuilder();
  builder.AppendFormat("");

  if (data.IsPreviousPage)
  {
  vs["pi"] = 1;
  builder.Append(Html.linkExtensions.Actionlink(html, "首页", action, controller, vs, null));
  builder.Append(" ");
  vs["pi"] = data.PageIndex - 1;
  builder.Append(Html.linkExtensions.Actionlink(html, "上一页", action, controller, vs, null));
  builder.Append(" ");

  }

  for (int i = start; i <= end; i++) //前后各显示5个数字页码
  {
  vs["pi"] = i;
  if (i == data.PageIndex)
  {
   builder.Append("" + i.ToString() + " ");
  }
  else
  {
   builder.Append(" ");

   builder.Append(Html.linkExtensions.Actionlink(html, i.ToString(), action, controller, vs, null));
  }
  }

  if (data.IsNextPage)
  {
  builder.Append(" ");

  vs["pi"] = data.PageIndex + 1;
  builder.Append(Html.linkExtensions.Actionlink(html, "下一页", action, controller, vs, null));
  builder.Append(" ");


  vs["pi"] = data.TotalPage;
  builder.Append(Html.linkExtensions.Actionlink(html, "末页", action, controller, vs, null));


  }
  builder.Append(" 每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页 ");
  return builder.ToString();
 }
 #endregion
 }
}

效果图:

5、源码下砸:jQuery.Ajax异步实现增删改查和分页

以上就是本文的全部内容,希望对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/97222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号