笔试考试系统 ____MVC Page分页使用及学生管理

1.今日内容

学生考试授权+学生信息编辑

学生管理模块(增删改查以及禁用):

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.Model;
using Exam.BLL;
using PagedList;
using Utility;
using Exam.UI.Filter;

namespace Exam.UI.Controllers
{
    [StudentFilter]
    public class StudentMannagerController : Controller
    {
        // GET: StudentMannager
        public ActionResult Index(int page = 1)
        {
            IPagedList list = StudentMannerService.GetList(page);
            return View(list);
        }
        public ActionResult Delete(int id)              
        {

            try
            {
                int res = StudentMannerService.RemoveStudent(id);
            }
            catch (Exception ex)
            {
                return Json(new { msg = "删除失败" + ex, success = false });

            }
            return Json(new { msg = "删除成功", success = true });

          
        }
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(string UserName, string RealName, string Phone)
        {
            Exam_User u = new Exam_User()
            {
                CreateTime = DateTime.Now,
                //学生账号默认密码123456
                PassWord = PassWordHelper.GetMD5("123456"),
                RealName = RealName,
                Phone = Phone,
                CreateName = "admin",
                States =true,
                 UserName=UserName, UserType=0
            };
            try
            {
                int res = StudentMannerService.InsertStudent(u);
            }
            catch (Exception ex)
            {
                return Json(new { msg = "添加失败"+ex, success = false });

            }
            return Json(new { msg = "添加成功", success = true });   
        }
        public ActionResult Edit(int id)
        {
         var data= StudentMannerService.FindStudentByID(id);

            return View(data);
        }
        [HttpPost]
        public ActionResult Edit(string uname, string Name, string phone,string id)
        {
            Exam_User u = new Exam_User { UserID= System.Convert.ToInt32(id), RealName= Name, UserName= uname, Phone = phone };
            try
            {
                StudentMannerService.Update(u);
            }
            catch (Exception ex)
            {
                return Json(new { msg = "修改失败" + ex, success = false });

            }
            return Json(new { msg = "修改成功", success = true });
        }


        /// <summary>
        /// 禁用账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Disable(int id)
        {
            try
            {
                StudentMannerService.Disable(id);
            }
            catch (Exception ex)
            {
                return Json(new { msg = "禁用失败" + ex, success = false });

            }
            return Json(new { msg = "禁用成功", success = true });

        }

        /// <summary>
        /// 启用账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Enable(int id)
        {
            try
            {
                StudentMannerService.Enable(id);
            }
            catch (Exception ex)
            {
                return Json(new { msg = "启用失败" + ex, success = false });

            }
            return Json(new { msg = "启用成功", success = true });

        }
    }
}

Service方法:

 public class StudentMannerService
    {
        /// <summary>
        /// 获取所有学生数据
        /// </summary>
        /// <param name="lmid"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static IPagedList GetList(int page = 1)
        {
            using (ExamSysDBContext db = new ExamSysDBContext())
            {
                int pagesize = 10;
                IPagedList list = db.Exam_User.Where(x => x.UserType == 0).OrderBy(x => x.UserID).ToPagedList(page, pagesize);
                return list;
            }
               
        }
        public static int InsertStudent(Exam_User user)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {

                dBContext.Exam_User.Add(user);
                return dBContext.SaveChanges();
            }
        }
        public static Exam_User FindStudentByID(int id)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {

                var data = dBContext.Exam_User.Where(x => x.UserID == id).FirstOrDefault();
                return data;
            }
        }
        public static int RemoveStudent(int id)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {
                var data = dBContext.Exam_User.Where(x => x.UserID == id).FirstOrDefault();
                dBContext.Exam_User.Remove(data);
                return dBContext.SaveChanges();
            }
                
        }
        public static int Update(Exam_User u)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {
                var data = dBContext.Exam_User.Where(x => x.UserID == u.UserID).FirstOrDefault();
                data.UserName = u.UserName;
                data.Phone = u.Phone;
                data.RealName = u.RealName;
                return dBContext.SaveChanges();
            }                
        }

        public static void Enable(int id)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {
                var data = dBContext.Exam_User.Where(x => x.UserID == id).FirstOrDefault();
                data.States = true;
                dBContext.SaveChanges();
            }
        }
        public static void Disable(int id)
        {
            using (ExamSysDBContext dBContext = new ExamSysDBContext())
            {
                var data = dBContext.Exam_User.Where(x => x.UserID == id).FirstOrDefault();
                data.States = false;
                dBContext.SaveChanges();
            }
        }
    }

页面效果:

 

 添加:

 禁用帐号

修改

 

3.遇到问题

4.解决方案

原文地址:https://www.cnblogs.com/zhangdongwei/p/13425498.html