ITOO级联删除主从表

来源:互联网 发布:星际淘宝网 编辑:程序博客网 时间:2024/06/03 16:01

    成绩系统有一个需求:数据字典有主从表,要求删除主表时候连同从表信息(跟牛腩中删除新闻时级联删除新闻评论相通),之前做好的那个版本中只删除主表数据,而且是按照主表数据的名称来删除,如果有重名的话还需做验证(感觉不太合理),现在需要做的是按照主表的ID来删除从表,主表的ID也能从页面拿到。

        我的思路:主表的html中引用主从表两个js,主从表的调用的buttonclick方法名称相同,但结果它只能找到执行其中的一个方法,结果主表删除后从表没有反应。

        后来李游给我思路:删除从表的js代码写在主表的触发的删除代码方法,结果和上面的相同,只执行了第一个方法。然后丹妹交流后,在主表的controller删除方法中调用从表的controller中删除方法,也是拿不到值。最后和刘杰、晓娜给我思路:修改主表的删除方法,传递主从两个实体为参数,可以将从页面获得主表的ID分别赋给他们,B层实现循环主从表的update方法,

#region DeleteTypeByTypeID--主表时同时删除从表信息(主从表两个作为参数)-李立平-2015-7-12 21:37:40        /// <summary>        /// 主表时同时删除从表信息(主从表两个作为参数)        /// </summary>        /// <param name="listtype">数据类型实体集合</param>        /// <param name="listValue">数据类型具体值实体集合</param>        /// <returns>返回是否删除成功,成功为True,失败为False</returns>        public bool DeleteTypeByTypeID(List<DictionaryTypeViewModel> listtype, List<DictionaryViewModel> listValue)        {            try            {    //配置文件加的注解,调用B层东西先实例化                DictionaryBll bll = new DictionaryBll();                //先删除从表,再删除依附的主表,循环将要删除的从表数据添加到集合中                for (int countValue = 0; countValue < listValue.Count; countValue++)                {                    //创建删除数据字典类型具体值的实体规则                    ResultDictionaryEntity tempValue=new ResultDictionaryEntity ()                    {                        isDeleted = 1 //删除后修改字段为1,伪删除,数据库还有该记录                    };                    Guid typeID= listtype[countValue].dictionaryTypeId;//主表ID赋给从表的外键                    //调用底层更新方法                    bll.CurrentDal.Update(tempValue, u => u.resultDictionaryTypeEntity_dictionaryTypeId == typeID, "isDeleted");                                  }                //bll.DbSession.SaveChanges();                //return true;                //循环将要删除的主表数据添加到集合中                for (int count = 0; count < listtype.Count; count++)                {                    //创建删除数据字典类型的实体规则                    ResultDictionaryTypeEntity temptype = new ResultDictionaryTypeEntity()                    {                        isDeleted = 1                    };                    //删除数据字典类型                    Guid typeId = listtype[count].dictionaryTypeId;                    //调用底层更新方法                    this.CurrentDal.Update(temptype, u => u.dictionaryTypeId == typeId, "isDeleted");                }                //保存信息,返回True                          this.DbSession.SaveChanges();                return true;            }            catch (Exception ex)            {                //抛出异常                throw new Exception(ex.Message);            }        }        #endregion

后来的controller(这里将主从表两个ViewModel添加的要删除的集合中,而且把主表ID分别赋给删除时的查询条件)

#region DeleteType --删除数据字典信息--李立平--2015-7-13 22:16:42        /// <summary>        /// 删除数据字典信息        /// </summary>        /// <param name="listData">数据字典集合</param>        /// <returns>bool值</returns>        //public bool DeleteType(IList<int> listData)        public bool DeleteType()        {                       //获得要添加的课程信息                           string strTypeName = Request.Form["dictionaryTypeId"];                       //通过切割的方式,把要添加的课程Guid取出            string[] strTypeNameList = strTypeName.Split(new Char[] { ',' });                                         //把要删除的id 存放到一个 实体集合中            for (int i = 0; i < strTypeNameList.Count(); i++)            {                Guid tempStr = new Guid(strTypeNameList[i]);                //调用底层方法Add                listType.Add(new DictionaryTypeViewModel());                listDictionary.Add(new DictionaryViewModel());                listType[i].dictionaryTypeId  = tempStr;                listDictionary[i].resultDictionaryTypeEntity_dictionaryTypeId = tempStr;            }            //调用服务端方法--根据Guid删除数据字典信息            bool IsDeleteSuccess = iDictionary.DeleteTypeByTypeID(listType,listDictionary);            return IsDeleteSuccess;        }        #endregion  

js代码

//批量删除--李立平--2015-7-13 22:18:31function del(){    //选中至少某一行数据    var rows = $('#dg').datagrid('getSelections');    //判断是否选择行    if (!rows || rows.length == 0) {        $.messager.alert('提醒', '请至少要选择一行数据!', 'Info');        return;    }    //获取表格选择行    var strId;        //循环给提交删除参数赋值(类型名称),n行数    $.each(rows, function (i, n) {        if (i == 0) {                       strId = "dictionaryTypeId=" + n.dictionaryTypeId;        } else {                       strId += "dictionaryTypeId=" + n.dictionaryTypeId;        }                   });     $.messager.confirm('提示', '是否删除选中数据?', function (r) {        //如果选否,则返回界面        if (!r) {            return;        }        if (strId != null) {            //提交,调用了controller中的DeleteType删除类型方法            $.ajax({                type: "POST",                async: false,                url: "/DictionaryType/DeleteType",                data:                     strId,                               success: function (data) {                    if (data == "True") {                        $.messager.show({                            title: '成功提示',                            msg: '成功删除信息!',                            timeout: 5000,                            showType: 'slide'                        });                        $('#dg').datagrid('reload');                    } else {                        $.messager.alert('提示信息', '删除失败,请重试!', 'error')                    }                }            });        }           });}

和其他的类似。

       两天解决了一个问题,这是一个过程,不断完善自己思考问题方式的过程,一步步积累。

1 0
原创粉丝点击