Exemple #1
0
        public async Task <IActionResult> Edit(int id, [Bind("CustomerId,CarId,Id,Timestamp")] Order order)
        {
            if (id != order.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(order);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderExists(order.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CarId"]      = new SelectList(_context.Cars, "Id", "Id", order.CarId);
            ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Id", order.CustomerId);
            return(View(order));
        }
        public async Task <IActionResult> Edit(int id, [Bind("FirstName,LastName,Id,Timestamp")] Customer customer)
        {
            if (id != customer.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Make,Color,PetName,Id,Timestamp")] Inventory inventory)
        {
            if (id != inventory.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var saved = false;
                while (!saved)
                {
                    try
                    {
                        _context.Update(inventory);
                        await _context.SaveChangesAsync();

                        saved = true;
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        if (!InventoryExists(inventory.Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            foreach (var entry in ex.Entries)
                            {
                                if (entry.Entity is Inventory)
                                {
                                    var proposedValues = entry.CurrentValues;
                                    var databaseValues = entry.GetDatabaseValues();

                                    foreach (var property in proposedValues.Properties)
                                    {
                                        var proposedValue = proposedValues[property];
                                        var databaseValue = databaseValues[property];

                                        //ToDo: decide which value should be written to the database
                                        proposedValues[property] = proposedValue; //value to be saved
                                    }

                                    //Refresh original values to bypass next concurrency check
                                    entry.OriginalValues.SetValues(databaseValues);
                                }
                                else
                                {
                                    throw new NotSupportedException("Don't know how to handle the concurrency conflicts for " + entry.Metadata.Name);
                                }
                            }
                        }
                    }
                }
                return(RedirectToAction(nameof(Index))); //saved
            }
            return(View(inventory));                     //invalid model status
        }