OA(二)编写基本的CURD

一.前情提要

接着之前的,新建一个控制器UserInfoController,并添加Index视图(前端框架用的easyUI)

以下是相关引用:

   
   

   
   
   
   
   
   

二.UI层搭建

UserInfoController

using OA.BLL;
using OA.IBLL;
using OA.Model;
using OA.Model.EnumType;
using OA.Model.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.WebApp.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/
        IUserInfoService userInfoService = new UserInfoService();
        public ActionResult Index()
        {
            return View();
        }

        #region 获取用户列表数据
        public ActionResult GetUserInfoList()
        {
            int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
            int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 5;
            int totalCount=0;
            //接收搜索条件
            string userName=Request["name"];
            string userRemark=Request["remark"];
            //构造搜索条件
            UserInfoSearch userInfoSearch = new UserInfoSearch
            { 
                userName=userName,
                userRemark=userRemark,
                pageIndex=pageIndex,
                pageSize=pageSize,
                TotalCount=totalCount
            };
            short delFlag = (short)DeleteEnumType.Normarl;
            //根据构建好的搜索条件完成搜索
            var userInfoList = userInfoService.LoadSearchEntities(userInfoSearch,delFlag);
            var temp = from u in userInfoList
                       select new
                       {
                           ID = u.ID,
                           UName = u.UName,
                           UPwd = u.UPwd,
                           Remark = u.Remark,
                           SubTime = u.SubTime
                       };
            return Json(new { rows = temp, total = userInfoSearch.TotalCount });
        }
        #endregion

        #region 删除用户数据
        public ActionResult DeleteUserInfo()
        {
            string strId = Request["strId"];
            string[] strIds = strId.Split(',');
            List list = new List();
            foreach (string id in strIds)
            {
                list.Add(Convert.ToInt32(id));
            }
            //将list集合存储的要删除的记录的编号传递到业务层
            if (userInfoService.DeleteEntities(list))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }
        #endregion

        #region 添加用户数据
        public ActionResult AddUserInfo(UserInfo userInfo)
        {
            userInfo.DelFlag = 0;
            userInfo.ModifiedOn = DateTime.Now;
            userInfo.SubTime = DateTime.Now;
            userInfoService.AddEntity(userInfo);
            userInfoService.CurrentDBSession.Savechanges();
            return Content("ok");
        }
        #endregion

        #region 展示要修改的数据
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);
            var userInfo = userInfoService.LoadEntities(u => u.ID == id).FirstOrDefault();
            return Json(userInfo, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 完成用户数据的更新
        public ActionResult EditUserInfo(UserInfo userInfo)
        {
            userInfo.ModifiedOn = DateTime.Now;
            if (userInfoService.EditEntity(userInfo))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }
        #endregion
    }
}

Index

@{
    Layout = null;
}





    
    Index
    
    

    
    
    
    
    
    

    


    
用户名:     备注: Search
@using (Ajax.BeginForm("AddUserInfo", "UserInfo", new { }, new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterAdd" }, new { id = "addForm" }) ) {
用户名
密码
备注
排序
}
@using (Ajax.BeginForm("EditUserInfo", "UserInfo", new { }, new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterEdit" }, new { id = "editForm" })) {
用户名
密码
备注
排序
}

三.业务层搭建(因为UserInfo有个批量删除,所以业务层需要编写自己特有的批量删除方法)

  IBLL

using IDAL;
using OA.Model;
using OA.Model.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.IBLL
{
    public interface IUserInfoService :IBaseService
    {
        bool DeleteEntities(List list);
        IQueryable LoadSearchEntities(UserInfoSearch userInfoSearch,short delFlag);
    }
}

BLL

using IDAL;
using OA.IBLL;
using OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.BLL
{
    public class UserInfoService : BaseService, IUserInfoService
    {
        public override void SetCurrentDal()
        {
            CurrentDal = this.CurrentDBSession.UserInfoDal;
        }
        /// 
        /// 批量删除多条用户数据
        /// 
        /// 
        /// 
        public bool DeleteEntities(List list)
        {
            var userInfoLis = this.CurrentDBSession.UserInfoDal.LoadEntities(u => list.Contains(u.ID));
            foreach (var userInfo in userInfoLis)
            {
                this.CurrentDBSession.UserInfoDal.DeleteEntity(userInfo);
            }
            return this.CurrentDBSession.Savechanges();
        }

        /// 
        /// 完成用户信息的搜索
        /// 
        /// 
        /// 
        public IQueryable LoadSearchEntities(Model.Search.UserInfoSearch userInfoSearch, short delFlag)
        {
            var temp = this.CurrentDBSession.UserInfoDal.LoadEntities(c => c.DelFlag == delFlag);
            //根据用户名来搜索
            if (!string.IsNullOrEmpty(userInfoSearch.userName))
            {
                temp = temp.Where(u => u.UName.Contains(userInfoSearch.userName));
            }
            //根据备注来搜索
            if (!string.IsNullOrEmpty(userInfoSearch.userRemark))
            {
                temp = temp.Where(u => u.Remark.Contains(userInfoSearch.userRemark));
            }
            userInfoSearch.TotalCount = temp.Count();
            return temp.OrderBy(u => u.ID).Skip((userInfoSearch.pageIndex - 1) * userInfoSearch.pageSize)
                   .Take(userInfoSearch.pageSize);
        }
    }
}

 

你可能感兴趣的:(.NET,JavaScript,JQuery,三层)