Пример #1
0
        public ActionResult Index()
        {
            var db = new AuctionsDataContext();
            var auctions = db.Auctions.ToArray();

            return View(auctions);
        }
 public ActionResult CategoryNavigation()
 {
     var db = new AuctionsDataContext();
     var categories = db.Auctions.Select(x => x.Category).Distinct();
     ViewBag.Categories = categories.ToArray();
     return PartialView();
 }
        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
            });
        }
Пример #4
0
        public ActionResult Auction(long id)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(id);

            return View(auction);
        }
        public ActionResult Auction(long id)
        {
            /* Commenting out this hard-coded Auction object, so we can use the find method
            //We can call the MvcAuction.model.Auction class without having to reference it using "using MvcAuction.model" in the header
            //This creates an instance of the Auction class and populates some of its properties with values
            var auction = new MvcAuction.Models.Auction()
            {
                Title = "Example Auction",
                Description = "This is an example Auction",
                StartTime = DateTime.Now,
                EndTime = DateTime.Now.AddDays(7),
                StartPrice = 1.00m,
                CurrentPrice = null,
            };
            */

            //To pass our Auction data to the view, we will store it in the ViewData dictionary using the key "Auction", then we will retrieve it in the view using the same key.*/
            //This method will be commented out to instead show how to pass the model directly to the view using the View helper property
            /*
            ViewData["Auction"] = auction;
            return View();
            */

            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(id);

            //This shows how to use the View helper property to send the model directly to the view
            return View(auction);
        }
 public ActionResult Auction(long id)
 {
     var db = new AuctionsDataContext();
     var auction = db.Auctions.Find(id);
     //ViewData["Auction"] = auction; alternate
     return View(auction);
 }
Пример #7
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);

            //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
                });
            }
        }
Пример #9
0
        public ActionResult CategoryListItems()
        {
            var db = new AuctionsDataContext();
            var categories = db.Auctions.Select(x => x.Category).Distinct();
            ViewBag.Categories = categories.ToArray();

            //to demostrate the cache concept of just the required data in a html page
            return PartialView();
        }
 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 Auction(long id = 1)
        {
            var db = new AuctionsDataContext();
            var auction = db.Auctions.Find(id);
            if(auction == null)
                return HttpNotFound();
               /* var auction = new MvcAuction.Models.Auction()
            {
                Id = id,
                Title = "Example Auction",
                StartTime = DateTime.Now,
                StartPrice = 25.43m,
                EndTime = DateTime.Now.AddDays(4),
                Description = " This the demo auction using model directly in view",
                CurrentPrice = null,
            };

            ViewData["Auction"] = auction;
            */
            return View(auction);
        }
 // ActionResult which is used to call a partial view for the sidebar which outputs a list of each category along with a link to the respective "CategoryResults" view
 public ActionResult SidebarCategories(string categories)
 {
     //Creates a new instace of AuctionsDataContext for storing the results of the following query
     var db = new AuctionsDataContext();
     //Queries the variable db to find the a list of distinct categories
     var categoryList = db.Auctions.Select(x => x.Category).Distinct();
     //Casts the categoryList to an Array and stores this in ViewBag.CategoryList so we can use it in the partial view
     ViewBag.CategoryList = categoryList.ToArray();
     //Calls the partial view "SidebarCategories"
     return PartialView();
 }
 public ActionResult SearchResults(string value)
 {
     //Creates a new instance of AuctionsDataContext for storing the results of the following query
     var db = new AuctionsDataContext();
     //Queries the variable db to find Auctions which have titles which contain the contents of the string "value" posted in the form
     var results = db.Auctions.Where(x => x.Title.Contains(value));
     //Uses the view helper method to send this variable to the "SearchResults" view for displaying
     return View(results);
 }
        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();
        }
 //This controller action is for displaying a list of auctions with a category matching the respective category link in the CategoryNavigation bar
 public ActionResult CategoryResults(string category)
 {
     //Creates a new instance of AuctionsDataContext for storing the results of the following query
     var db = new AuctionsDataContext();
     //Queries the variable db to find Auctions which have categories that match the "category" string passed into the controller action
     var results = db.Auctions.Where(x => x.Category.Contains(category));
     //Uses the view helper method to send this variable to the "CategoryResults" view for displaying
     return View(results);
 }
        public ActionResult Index()
        {
            /* Commenting out the hard-coded Auctions, so we can display our database elements
            //creates an array of Auction objects named auctions
            var auctions = new[] {
                new Models.Auction()
                {
                    Title = "Example Auction #1",
                    Description = "This is an example Auction",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(7),
                    StartPrice = 1.00m,
                    CurrentPrice = null,
                },
                new Models.Auction()
                {
                    Title = "Example Auction #2",
                    Description = "This is a second Auction",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(7),
                    StartPrice = 1.00m,
                    CurrentPrice = 30m,
                }
            };
            */

            //Creates a new instance of AuctionsDataContext (if it doesn't already exist)
            var db = new AuctionsDataContext();
            //Uses the toArray to execute a SQL call at this point so we can get the list before passing them to the view
            var auctions = db.Auctions.ToArray();

            return View(auctions);
        }
        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 Index()
        {
            var db = new AuctionsDataContext();
            var auctions = db.Auctions.ToArray();
            /*var auctions = new[] {
            new Models.Auction () {
                Title = "1. Auction",
                StartTime = DateTime.Now,
                StartPrice = 25.43m,
                EndTime = DateTime.Now.AddDays(4),
                Description = " This the demo auction using model directly in view",
                CurrentPrice = null,
            },
            new Models.Auction () {
               Title = "2. Auction",
                StartTime = DateTime.Now,
                StartPrice = 25.43m,
                EndTime = DateTime.Now.AddDays(2),
                Description = " This the demo auction using model directly in view",
                CurrentPrice = 22.34m,
            },
            new Models.Auction () {
               Title = "3. Auction",
                StartTime = DateTime.Now,
                StartPrice = 25.43m,
                EndTime = DateTime.Now.AddDays(1),
                Description = " Third Auction",
                CurrentPrice = 56.98m,
            }
            }; */

            return View(auctions);
        }