public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //**********************Authorization*******************************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User,
                                                                         ProductCategory, Operations.Update);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }
            //******************************************************************************************************

            var productCategoryToUpdate = await _context.ProductCategory
                                          .FirstOrDefaultAsync(pc => pc.ProductCategoryID == id);

            if (productCategoryToUpdate == null)
            {
                HandleDeletedProductCategory();
            }

            _context.Entry(productCategoryToUpdate).Property("RowVersion")
            .OriginalValue = ProductCategory.RowVersion;

            if (await TryUpdateModelAsync <ProductCategory>(
                    productCategoryToUpdate,
                    "productCategory",
                    pc => pc.ProductCategoryID, pc => pc.Name))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToPage("./Index"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntries = ex.Entries.Single();
                    var clientValues     = (ProductCategory)exceptionEntries.Entity;
                    var databaseEntry    = exceptionEntries.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The department was deleted by another user.");
                        return(Page());
                    }
                    var dbValues = (ProductCategory)databaseEntry.ToObject();
                    SetDbErrorMessage(dbValues, clientValues, _context);
                    ProductCategory.RowVersion = (byte[])dbValues.RowVersion;
                    ModelState.Remove("ProductCategory.RowVersion");
                }
            }
            return(Page());
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync(Guid id, string submitProduct)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var productToUpdate = await _context.Product.FindAsync(id);

            if (productToUpdate == null)
            {
                HandleDeletedProduct();
            }

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

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "product",
                    p => p.ProductName, p => p.ProductDescr1,
                    p => p.ProductDescr2_Wt_Vol, p => p.Wholesaler, p => p.ProductCategoryID))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    if (submitProduct == "SaveAndEditVendors")
                    {
                        return(RedirectToPage("./AddVendorsToProduct", new { id }));
                    }
                    else
                    {
                        return(RedirectToPage("./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. " +
                                                 "The product was deleted by another user.");
                        return(Page());
                    }

                    var dbValues = (Product)databaseEntry.ToObject();
                    await SetDbErrorMessage(dbValues, clientValues, _context);

                    Product.RowVersion = (byte[])dbValues.RowVersion;
                    ModelState.Remove("Product.RowVersion");
                }
                ProductCategorySL = new SelectList(Context.ProductCategory,
                                                   "ProductCategoryID", "Name", productToUpdate.ProductCategoryID);
            }
            return(Page());
        }
Beispiel #3
0
        public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //****************Authorization*****************************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User, Vendor, Operations.Update);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }
            //**********************************************************************************************

            var vendorToUpdate = await _context.Vendor
                                 .FirstOrDefaultAsync(v => v.VendorID == id);

            if (vendorToUpdate == null)
            {
                return(HandleDeletedVendor());
            }

            _context.Entry(vendorToUpdate).Property("RowVersion").OriginalValue = Vendor.RowVersion;

            if (await TryUpdateModelAsync <Vendor>(
                    vendorToUpdate,
                    "vendor",
                    v => v.VendorID, v => v.VendorName, v => v.OwnerID, v => v.VendorAddress1,
                    v => v.VendorAddress2))
            {
                try
                {
                    //await _context.SaveChangesAsync();
                    return(RedirectToPage("./Index"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Vendor)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The department was deleted by another user.");
                        return(Page());
                    }
                    var dbValues = (Vendor)databaseEntry.ToObject();
                    setDbErrorMessage(dbValues, clientValues, _context);
                    Vendor.RowVersion = (byte[])dbValues.RowVersion;
                    ModelState.Remove("Vendor.RowVersion");
                }
            }
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(string CurrentSort, string CurrentFilter,
                                                      int PageIndex, IEnumerable <Offering> Offering)
        {
            foreach (var offering in Offering)
            {
                if (!ModelState.IsValid)
                {
                    return(Page());
                }
                var offeringToUpdate = await _context.Offering
                                       .FirstOrDefaultAsync(o => o.OfferingID == offering.OfferingID);

                _context.Entry(offeringToUpdate).Property("RowVersion").OriginalValue = offering.RowVersion;

                offeringToUpdate.ProductPricePerWeight = offering.ProductPricePerWeight;
                offeringToUpdate.AsOfDate = offering.AsOfDate;

                if (await TryUpdateModelAsync <Offering>(
                        offeringToUpdate,
                        "offering",
                        o => o.ProductPricePerWeight, o => o.AsOfDate))
                {
                }
            }
            try
            {
                await _context.SaveChangesAsync();

                await ArchiveOffering.Archive(_context);

                Message = null;
                return(RedirectToPage("./Offering", new
                {
                    sortOrder = CurrentSort,
                    currentFilter = CurrentFilter,
                    pageIndex = PageIndex
                }));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var exceptionEntry = ex.Entries.Single();
                var clientValues   = (Offering)exceptionEntry.Entity;
                var databaseEntry  = exceptionEntry.GetDatabaseValues();

                var dbValues = (Offering)databaseEntry.ToObject();
                setDbErrorMessage(dbValues, clientValues, _context);

                ModelState.Remove("RowVersion");
                string message = "The record you attempted to edit "
                                 + "was modified by another user after you. The "
                                 + "edit operation was canceled and the current values in the database "
                                 + "have been displayed. TO EDIT THIS RECORD, RE-ENTER YOUR VALUES."
                                 + "Otherwise the other user would not be aware that you had overwritten those entries";

                return(RedirectToPage("./Offering", new
                {
                    message,
                    sortOrder = CurrentSort,
                    currentFilter = CurrentFilter,
                    pageIndex = PageIndex
                }));
            }
        }