Exemplo n.º 1
0
Arquivo: Abc.cs Projeto: chenmsg/sujin
        /// <summary>
        /// 配合前台ajax_form_submit_json.js文件的提交方式,封装成简易版添加,更改方法
        /// </summary>
        /// <param name="rval">表单集合字符串</param>
        /// <returns></returns>
        public static string AjaxEdit(Abc entity)
        {
            string result = "操作有误";

            try
            {
                if (entity != null)
                {
                    using (BaseEntityDbContext <Abc> db = new BaseEntityDbContext <Abc>())
                    {
                        if (entity.id == 0)                                       //添加操作
                        {
                            db.BaseDbSet.Add(entity);
                            if (db.SaveChanges() > 0)
                            {
                                result = "保存成功";
                            }
                        }
                        else if (entity.id > 0)                                             //更新操作
                        {
                            Abc edit = db.BaseDbSet.FirstOrDefault(o => o.id == entity.id); //查询
                            edit = CustomHelper.ConvertToModel(entity, edit);               //赋值
                            DbEntityEntry entry = db.Entry(edit);
                            //if (entry.State == EntityState.Detached)
                            //{
                            entry.State = EntityState.Modified;//通过更改 状态为EntityState.Modified 然后再保存 来实现更新操作
                            //}
                            result = "您没有做任何更改";
                            if (db.SaveChanges() > 0)
                            {
                                result = "保存成功";
                            }
                            else
                            {
                                result = "保存时遇到问题";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = "操作有误" + ex.Message;
                log.Error("操作有误", ex);
                return(result);
            }
            return(result);
        }