Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(string id, [Bind("firstName,lastName,gender,deletedByAdmin,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
        {
            if (id != user.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(user);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserExists(user.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                bool isAdmin = await this.userManager.IsInRoleAsync(user, "Admin");

                if (isAdmin)
                {
                    return(RedirectToAction(nameof(UserController.Index), "User"));
                }
                else
                {
                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
            }
            return(View(user));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, EditAuctionModel auctionModel)
        {
            var auction = await _context.auctions.FindAsync(id);


            if (id != auction.Id)
            {
                return(NotFound());
            }

            if (auction.state != Auction.AuctionState.DRAFT)
            {
                auctionModel.base64Data = Convert.ToBase64String(auction.image);
                ModelState.AddModelError("", "This auction can not be edited.");
                ModelState.AddModelError("", "Sorry!");
                return(View(auctionModel));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    auction.name         = auctionModel.name;
                    auction.description  = auctionModel.description;
                    auction.startPrice   = auctionModel.startPrice;
                    auction.currentPrice = auctionModel.startPrice;
                    auction.openDate     = auctionModel.openDate;
                    auction.closeDate    = auctionModel.closeDate;

                    if (auctionModel.image != null)
                    {
                        using (BinaryReader reader = new BinaryReader(auctionModel.image.OpenReadStream( ))) {
                            auction.image = reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length));
                        };
                    }

                    _context.Update(auction);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuctionExists(auction.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                if (User.FindFirst(ClaimTypes.Role).Value == "Admin")
                {
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    return(RedirectToAction(nameof(MyAuctions), "Auction"));
                }
            }

            return(View(auctionModel));
        }