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); 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); //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 Create([Bind(Exclude="CurrentPrice")]Models.Auction auction) { if (ModelState.IsValid) { //Saving inside database var db = new AuctionsDataContext(); db.Auctions.Add(auction); db.SaveChanges(); return RedirectToAction("Index"); } return Create(); }
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); */ }
public ActionResult Create([Bind(Exclude="CurrentPrice")]Models.Auction auction) { /*Now that we have created validation using Data Annotations, these validation statements will be commented out //This IF statement makes sure a user enters the required title and throws an error if they don't if (string.IsNullOrWhiteSpace(auction.Title)) { //Adds a model error to the Model state. The parameters are the property name key and the error message ModelState.AddModelError("Title", "Title is required!"); } //This IF statement makes sure the title is between 5 and 200 characters long else if (auction.Title.Length < 5 || auction.Title.Length > 200) { ModelState.AddModelError("Title", "Title must be between 5 and 20 characters long!"); } */ //If the modelstate is valid (no errors), redirect the action to "Index" if (ModelState.IsValid) { //Creates a new instance of AuctionsDataContext, adds new object, and saves changes var db = new AuctionsDataContext(); db.Auctions.Add(auction); db.SaveChanges(); return RedirectToAction("Index"); } //calls the Create function again if the user's input has errors! return Create(); }
public ActionResult Create([Bind(Exclude="CurrentPrice")]Models.Auction auction) { /* if (string.IsNullOrWhiteSpace(auction.Title)) { ModelState.AddModelError("Title","Title is expected!"); } else if (auction.Title.Length < 5 || auction.Title.Length > 200) { ModelState.AddModelError("Title", "Title should be between 5 and 200 characters"); } */ if (ModelState.IsValid) { //save to data base //Response.Write("<script type='text/javascript'> alert('Saved to the database')</script>"); var db = new AuctionsDataContext(); db.Auctions.Add(auction); db.SaveChanges(); return RedirectToAction("Index"); } return Create(); }