Exemplo n.º 1
0
        public async Task <IActionResult> PutCampaign(int id, Campaign campaign)
        {
            if (id != campaign.CampaignId)
            {
                return(BadRequest());
            }

            _context.Entry(campaign).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CampaignExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PutUser(int id, VitecUser user)
        {
            if (id != user.VitecUserId)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(int?id, byte[] rowVersion)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var productToUpdate = await _context.Product /*.Include(i => i.Administrator)*/.FirstOrDefaultAsync(m => m.ID == id);

            if (productToUpdate == null)
            {
                Product deletedProduct = new Product();
                await TryUpdateModelAsync(deletedProduct);

                ModelState.AddModelError(string.Empty,
                                         "Unable to save changes. The product was deleted by another user.");
                ViewData["ID"] = new SelectList(_context.Product, "ID", "Name", deletedProduct.ID);
                return(View(deletedProduct));
            }

            _context.Entry(productToUpdate).Property("RowVersion").OriginalValue = rowVersion;

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "",
                    s => s.Name, s => s.Price, s => s.Description, s => s.ID))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Product)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty,
                                                 "Unable to save changes. The product was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Product)databaseEntry.ToObject();

                        if (databaseValues.Name != clientValues.Name)
                        {
                            ModelState.AddModelError("Name", $"Current value: {databaseValues.Name}");
                        }
                        if (databaseValues.Price != clientValues.Price)
                        {
                            ModelState.AddModelError("Price", $"Current value: {databaseValues.Price:c}");
                        }
                        if (databaseValues.Description != clientValues.Description)
                        {
                            ModelState.AddModelError("Description", $"Current value: {databaseValues.Description:d}");
                        }

                        ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                 + "was modified by another user after you got the original value. The "
                                                 + "edit operation was canceled and the current values in the database "
                                                 + "have been displayed. If you still want to edit this record, click "
                                                 + "the Save button again. Otherwise click the Back to List hyperlink.");
                        productToUpdate.RowVersion = (byte[])databaseValues.RowVersion;
                        ModelState.Remove("RowVersion");
                    }
                }
            }
            ViewData["ID"] = new SelectList(_context.Product, "ID", "Name", productToUpdate.ID);
            return(View(productToUpdate));
        }