public async Task <ServiceResponseBase> Update(PresetFunctionViewModel vm)
        {
            try
            {
                var isExist = await _context.ReSetFunction.AnyAsync(o => o.Name == vm.Name &&
                                                                    o.Id != vm.Id);

                if (isExist)
                {
                    return(new ServiceResponseBase()
                    {
                        Status = Status.error, Message = "存在重复编码!"
                    });
                }
                var query = await _context.ReSetFunction.FirstOrDefaultAsync(o => o.Id == vm.Id);

                Mapper.Map(vm, query); //不能使用Mapper.Map<ReSetFunction>(vm),会创建一个新的实例
                _context.Entry(query).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(new ServiceResponseBase()
                {
                    Status = Status.ok
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                return(new ServiceResponseBase()
                {
                    Status = Status.error, Message = ex.Message
                });
            }
        }
Example #2
0
        public ActionResult Create(PresetFunctionViewModel collection)
        {
            try
            {
                // TODO: Add insert logic here

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Example #3
0
        public async Task <ActionResult> Edit(int id, PresetFunctionViewModel vm)
        {
            try
            {
                var result = await _presetFunctionServices.Update(vm);

                if (result.Status != Status.ok)
                {
                    return(Content($"<script>alert('保存失败:{result.Message}');history.back();</script>"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }