Exemplo n.º 1
0
        public ActionResult Bid(Bid bid)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(bid.AuctionId);

            if (auction == null)
            {
                ModelState.AddModelError("AuctionId", "Auction not found!");
            }
            else if (auction.CurrentPrice >= bid.Amount)
            {
                ModelState.AddModelError("Amount", "Bid amount must exceed current bid");
            }
            else
            {
                bid.Username = User.Identity.Name;
                auction.Bids.Add(bid);
                auction.CurrentPrice = bid.Amount;
                db.SaveChanges();
            }

            if (!Request.IsAjaxRequest())
                return RedirectToAction("Auction", new { id = bid.AuctionId });

            var httpStatus = ModelState.IsValid ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
            return new HttpStatusCodeResult(httpStatus);
        }
        public ActionResult Bid(Bid bid)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(bid.AuctionId);

            if(auction == null) {
                ModelState.AddModelError("ActionId", "Auction not found!");
            } else if(auction.CurrentPrice >= bid.Amount){
                ModelState.AddModelError("Amount", "Bid amount must exceed current bid");
            } else {
                bid.Username = User.Identity.Name;
                auction.Bids.Add(bid);
                auction.CurrentPrice = bid.Amount;
                db.SaveChanges();
            }

            if(!Request.IsAjaxRequest())
                return RedirectToAction("Auction", new {id = bid.AuctionId});

               // Send JSOn response
               return Json(new {
                CurrentPrice = bid.Amount.ToString("C"),
                BidCount = auction.BidCount
            });
        }
        public ActionResult Bid(Bid bid)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(bid.AuctionId);

            //Checks to see if the auction exists first
            if (auction == null)
            {
                ModelState.AddModelError("AuctionId", "Auction not found!");
            }
            //Checks to see whether the new bid is actually greater than the current price
            else if (auction.CurrentPrice >= bid.Amount)
            {
                ModelState.AddModelError("Amount", "Bid amount must exceed current bid");
            }
            /*If there are no errors (model state is valid), then set bid.username to current user,
                add bid, update current price, and save changes to db*/
            else
            {
                bid.Username = User.Identity.Name;
                auction.Bids.Add(bid);
                auction.CurrentPrice = bid.Amount;
                db.SaveChanges();
            }

            //Checks to see whether the request is Ajax or not.  If it isn't an Ajax Request, the controller is free to return the redirect action
            //If it is an Ajax request, the controller action should return an http status result that jQuery can analyze to see if the request was successful or not
            if (!Request.IsAjaxRequest())
                return RedirectToAction("Auction", new { id = bid.AuctionId });

            /* This will be commented out because we are going to use the partial view method to update the CurrentPrice
            //Uses httpStatus to check if the request was successful or not and display the appropriate message
            var httpStatus = ModelState.IsValid ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
            return new HttpStatusCodeResult(httpStatus);
            */

            //Uses PartialView helper to call the "_CurrentPrice" page after the Ajax Request
            //This will be commented out to instead show how to use the JSON helper method to accomplish a similar function
            /*
                return PartialView("_CurrentPrice", auction);
            */

            //Uses JSON helper method to update only the values that we want changed instead of calling a partial view to change the HTML
            //I have added this if statement (not in lab!) to make sure the JSON doesn't update if there is an error in the modelstate
            //If there is an error (auction not found/bid < current price), the Ajax method isn't posted successfully and an error is displayed
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Auction", bid.AuctionId);
            }
            else
            {
                return Json(new
                {
                    CurrentPrice = bid.Amount.ToString("C"),
                    BidCount = auction.BidCount
                });
            }
        }
        public ActionResult Bid(Bid bid)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(bid.AuctionId);
            if (auction == null)
            {
                ModelState.AddModelError("AuctionId", "Auction not found!");
            }
            else if (auction.CurrentPrice >= bid.Amount)
            {
                ModelState.AddModelError("Amount", "New Bid should exceed the current price");
            }
            else
            {
                bid.UserName = User.Identity.Name;
                auction.Bids.Add(bid);
                auction.CurrentPrice = bid.Amount;
                db.SaveChanges();
            }

            //return RedirectToAction("Auction", new { id = bid.AuctionId });

            if (!Request.IsAjaxRequest()) // if not an ajax request, return norml return
                return RedirectToAction("Auction", new { id = bid.AuctionId });

            if (ModelState.IsValid)
            {
                return Json(new
                {
                    CurrentPrice = bid.Amount.ToString("C"),
                    BidCount = auction.BidCount
                });
            }
            else
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            /*
            // to return a partial view
            if (ModelState.IsValid)
                return PartialView("_CurrentPricePartial", auction);
            else
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
             */

            /* this code is just only for response
               // if ajax request sent an ajax response
            var httpStatus = ModelState.IsValid ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
            return new HttpStatusCodeResult(httpStatus);
             */
        }