EF增删改查

1创建项目点击MVC具体参照登录操作

2在表示层的控制器文件写代码

2.1添加和编辑一起,编辑需要从数据库获取数据放在页面上修改,所以要查询要在业务层创建一个类写查询代码

 //查询单表用lamdam表达式
 public class ModueService : BaseService<InfoManager>
    {
        public InfoManager SelectInfoMangers(int id)
        {
            return Query(a => a.ID == id);

        }
    }

 

2.1.1创建一个AdminUserController控制器(命名随意)

  //添加
public JsonResult Add(InfoManager infoManager)
        {
            ModueService modueService = new ModueService();
            OperateResult operateResult = new OperateResult();
            operateResult.Success = modueService.Add(infoManager);
            return Json(operateResult);
        }

        //修改
        public JsonResult Update(InfoManager infoManager)
        {
            ModueService modueService = new ModueService();
            OperateResult operateResult = new OperateResult();
            operateResult.Success = modueService.Update(infoManager);
            return Json(operateResult);
        }

2.1.2再去Home控制器下的Add视图传值判断

  public ActionResult articleadd(int id)
        {
            //UserIndex页面传一个id判断改添加操作还是修改操作
            if (id != 0)
            {
                ModueService modueService = new ModueService();
                //读取信息调用SelectUserInfo方法
                ViewBag.info = modueService.SelectInfoMangers(id);
            }
            return View();
 
        }

2.1.3页面代码Ajax

   $(function () {

            //提交点击事件
    $("#submit").click(function () {
        //实例化对象
        var userInfo = {};
        userInfo.ID = $("#id").val();
        userInfo.Title = $("#articletitle").val();
        userInfo.Sort = $("#sort").val();
        userInfo.Source = $("#sources").val()
        userInfo.Browser = $("#browser").val();
        userInfo.Status = $("#status").val();
       

        //判断Id是否为空,true则进行添加操作
        if ($("#id").val() == 0) {
            //温馨提示:是否添加用户
            layer.confirm('是否添加用户', function (index) {
                //ajax提交到后台进行添加用户操作
                $.ajax({
                    type: "post",
                    url: "/AdminUser/Add",
                    data: userInfo,
                    success: function (operateResult) {
                        if (operateResult.Success) {
                            //温馨提示:添加成功
                            layer.msg('添加成功!', {
                                icon: 1,
                                time: 1000
                            });
                        } else {
                            //温馨提示:添加失败
                            layer.msg('添加失败!', {
                                icon: 1,
                                time: 1000
                            });
                        }
                    },
                })
            })
            //如果Id是不为空,false则进行修改操作
        } else {
            //温馨提示:是否修改用户
            layer.confirm('是否修改用户', function (index) {
                //ajax提交到后台进行修改用户操作
                $.ajax({
                    type: "post",
                    url: "/AdminUser/Update",
                    data: userInfo,
                    success: function (operateResult) {
                        //温馨提示:修改成功
                        if (operateResult.Success) {
                            layer.msg('修改成功!', {
                                icon: 1,
                                time: 1000
                            });
                        } else {
                            //温馨提示:添加失败
                            layer.msg('修改失败!', {
                                icon: 1,
                                time: 1000
                            });
                        }
                    }
                })
            })
        }    
    })

3.删除在控制器代码

 

  //删除单条数据
        public JsonResult DELETE(InfoManager infoManager)
        {
            ModueService modueService = new ModueService();
            OperateResult operateResult = new OperateResult();
            operateResult.Success = modueService.DELETE(infoManager);
            return Json(operateResult);
        }

3.1页面代码

layui.use('table', function () {
        var table = layui.table
            , form = layui.form;  //启用
        table.render({
            elem: '#test'
            , url: '/AdminUser/GetMoudelList/'
            , cellMinWidth: 80
            , cols: [[
                { type: 'checkbox' }
                , { field: 'ID', width: 180, align: 'center', title: 'Id', sort: true }
                , { field: 'Title', width: 180, align: 'center', title: '标题' }
                , { field: 'Sort', width: 180, align: 'center', title: '分类' }
                , { field: 'Source', width: 180, align: 'center', title: '来源' }
                , { field: 'Browser', width: 180, align: 'center', title: '浏览' }
                , { field: 'Status', width: 180, align: 'center', title: '状态' }
                , { fixed: 'right', title: '操作', width: 208, align: 'center', toolbar: '#barDemo' }
            ]]
            , page: true
            , id: 'testReload'
        });
        table.on('tool(demo)', function (obj) {
            var AdminUser = obj.data;
            var ID = AdminUser.ID;
            if (obj.event === 'del') {
                //删除
                layer.confirm('确定删除吗?', function (index) {
                    $.ajax({
                        url: "/AdminUser/DELETE?Id=" + ID,
                        type: "Post",
                        success: function (data) {
                            if (data.Success) {
                                layer.msg('删除成功!', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 2000
                                }, function () {
                                    location.reload();//刷新页面
                                    layer.close(index);
                                });
                            }
                            else {
                                layer.msg('删除失败!', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 2000
                                });
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/Home/articleadd?id=" + ID;
            };
        })
    })

4.查询

 //分页查询
        public JsonResult GetMoudelList(int page, int limit)
        {
            ModueService modueService = new ModueService();
            ResultDataSet<InfoManager> resultDataSet = new ResultDataSet<InfoManager>();
            resultDataSet.code = 0;
            resultDataSet.msg = string.Empty;
            resultDataSet.count = 0;
            int count = 0;
            List<InfoManager> modules = new List<InfoManager>();
            Expression<Func<InfoManager, bool>> whereLambda = a =>true;
            Expression<Func<InfoManager, int>> orderbyLambda = a => a.ID;
            resultDataSet.data = modueService.QuerypageList(page, limit, whereLambda, orderbyLambda, out count);
            return Json(resultDataSet,JsonRequestBehavior.AllowGet);
        }

4.1页面查询代码

layui.use('table', function () {
        var table = layui.table
            , form = layui.form;  //启用
        table.render({
            elem: '#test'
            , url: '/AdminUser/GetMoudelList/'
            , cellMinWidth: 80
            , cols: [[
                { type: 'checkbox' }
                , { field: 'ID', width: 180, align: 'center', title: 'Id', sort: true }
                , { field: 'Title', width: 180, align: 'center', title: '标题' }
                , { field: 'Sort', width: 180, align: 'center', title: '分类' }
                , { field: 'Source', width: 180, align: 'center', title: '来源' }
                , { field: 'Browser', width: 180, align: 'center', title: '浏览' }
                , { field: 'Status', width: 180, align: 'center', title: '状态' }
                , { fixed: 'right', title: '操作', width: 208, align: 'center', toolbar: '#barDemo' }
            ]]
            , page: true
            , id: 'testReload'
        });
        table.on('tool(demo)', function (obj) {
            var AdminUser = obj.data;
            var ID = AdminUser.ID;
            if (obj.event === 'del') {
                //删除
                layer.confirm('确定删除吗?', function (index) {
                    $.ajax({
                        url: "/AdminUser/DELETE?Id=" + ID,
                        type: "Post",
                        success: function (data) {
                            if (data.Success) {
                                layer.msg('删除成功!', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 2000
                                }, function () {
                                    location.reload();//刷新页面
                                    layer.close(index);
                                });
                            }
                            else {
                                layer.msg('删除失败!', {
                                    title: '提示框',
                                    icon: 1,
                                    time: 2000
                                });
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/Home/articleadd?id=" + ID;
            };
        })
    })


</script>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值