public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Age,Email")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Esempio n. 2
0
        public async Task <IActionResult> Add(KeyVM model)
        {
            if (model == null)
            {
                return(View(model));
            }
            if (string.IsNullOrWhiteSpace(model.Base))
            {
                return(View(model));
            }
            if (string.IsNullOrWhiteSpace(model.Main))
            {
                return(View(model));
            }

            try
            {
                var key = new LocalizationKey()
                {
                    Base    = model.Base,
                    Comment = model.Comment,
                    Key     = model.Main
                };

                _context.Add(key);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Edit), new { baseKey = model.Base, mainKey = model.Main, saveResult = SaveResult.Success }));
            }
            catch (DbUpdateException ex) when((ex.InnerException as System.Data.SqlClient.SqlException)?.Number == 2601)
            {
                _context.ChangeTracker.Entries().ToList().ForEach(e => e.State = EntityState.Detached);
                model.SaveResult = SaveResult.Duplicate;
                return(View(model));
            }
            catch (Exception)
            {
                model.SaveResult = SaveResult.UnknownFailure;
                return(View(model));
            }
        }