コード例 #1
0
        public async Task <IActionResult> Create([Bind("ReviewId,LicenceId,BadgeId,CreateDate,UserIp")] Review review)
        {
            //"badges" used for the View - all the field of a Badge record are now visible in the dropdown
            var badges = _context.Badge.Select(m => new { Text = m.Title + " -- " + (BadgeType)Enum.ToObject(typeof(BadgeType), m.Type) + " -- " + m.Description, Value = m.BadgeId }).ToList();
            //userip  - local variable used to get the IP of the user to be then assigned to the UserIp property of Review
            string userip = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(1).ToString();

            review.UserIp = userip;
            //spam - used to determine if the same user (same ip) has already reviewed the same Car (same LicenseId) in the last 72 hours
            bool spam = _context.Review.Any(s => s.LicenceId == review.LicenceId && s.UserIp == review.UserIp && (review.CreateDate.Subtract(s.CreateDate).TotalDays < 3.0d));

            if ((ModelState.IsValid) && (!spam))
            {
                _context.Add(review);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            if (spam == true)
            {
                return(RedirectToAction("CreateExists", new { saveChangesError = true })); //for generating a spam error message in the View
            }
            ViewData["BadgeId"]   = new SelectList(badges, "Value", "Text");
            ViewData["LicenceId"] = new SelectList(_context.Cars, "LicenceId", "Plate", review.LicenceId);
            return(View(review));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("BadgeId,Title,Type,Description")] Badge badge)
        {
            if (ModelState.IsValid)
            {
                _context.Add(badge);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(badge));
        }
コード例 #3
0
 public async Task <IActionResult> Create([Bind("LicenceId,Plate")] Cars cars)
 {
     if (ModelState.IsValid)
     {
         _context.Add(cars);
         try
         {
             await _context.SaveChangesAsync();
         }
         catch (Exception)                                                                       //improvised way to make sure only one record of a particular plate exists
         {
             return(RedirectToAction("CreateExists", "Reviews", new { sequence = cars.Plate })); //redirects to the Create Review View for when the plate already exists in the db
         }
         return(RedirectToAction("CreateNew", "Reviews", new { id = cars.LicenceId }));          //redirects to the Create Review View for when the plate does not yet exists in the db
     }
     return(RedirectToAction("Index", "Upload", new { saveChangesError = true }));               //only called when the user does not select a plate in the view  - redirects to the Upload View so they can try again
 }