public ActionResult DeleteConfirmed(int id) { AdsViewContact adsViewContact = db.AdsViewContacts.FirstOrDefault(c => c.Advert.Id == id); if (adsViewContact != null) { db.AdsViewContacts.Remove(adsViewContact); } AdsViewFile adsViewFile = db.AdsViewFile.FirstOrDefault(c => c.AdId == id); if (adsViewFile != null) { db.AdsViewFile.Remove(adsViewFile); } AdsViewAdverts adsViewAdverts = db.AdsViewAdverts.Find(id); if (adsViewAdverts != null) { db.AdsViewAdverts.Remove(adsViewAdverts); } db.SaveChanges(); return(RedirectToAction(nameof(MyAdverts))); }
//public ActionResult Create([Bind(Include = "Id")] FormCollection adsViewAdverts) //public ActionResult Create(FormCollection adsViewAdverts) public ActionResult Create([Bind(Include = "Id")] FormCollection adsViewAdverts, HttpPostedFileBase upload) { var authUser = User.Identity.GetUserId(); Guid confirmation = Guid.NewGuid(); bool contactInformationFailure = false; bool uploadedFileFailure = false; // Get the current date. DateTime thisDay = DateTime.Today; AdsViewAdverts advert = new AdsViewAdverts(); advert.CategoryId = Int32.Parse(adsViewAdverts["Category"]); advert.Status = 0; advert.DateLastModified = thisDay; advert.DatePublished = Convert.ToDateTime(adsViewAdverts["DatePublished"]); advert.ExpiryDate = Convert.ToDateTime(adsViewAdverts["ExpiryDate"]); advert.Title = adsViewAdverts["Title"]; advert.Description = adsViewAdverts["Description"]; advert.Price = Convert.ToDecimal(adsViewAdverts["Price"]); advert.DateAdvertAdded = thisDay; advert.DateOfDeleteAdvert = Convert.ToDateTime("31/12/2016 12:00:00 AM"); advert.ConfirmationCode = Guid.NewGuid().ToString().Substring(0, 9); advert.AdvertTypeId = Int32.Parse(adsViewAdverts["AdvertType"]); advert.LocationId = Int32.Parse(adsViewAdverts["Location"]); advert.SubCategoryId = Int32.Parse(adsViewAdverts["SubCategory"]); advert.User = authUser; if (ModelState.IsValid) { db.AdsViewAdverts.Add(advert); try { db.SaveChanges(); int adId = GetAdId(advert.ConfirmationCode.ToString()); if (adId != 0) { AdsViewContact contact = new AdsViewContact(); AdsViewFile attachment = new AdsViewFile(); contact.Id = adId; contact.ContactName = adsViewAdverts["ContactName"]; contact.ContactPhoneNumber = adsViewAdverts["ContactPhoneNumber"]; contact.ContactPhysicalAddress = adsViewAdverts["ContactPhysicalAddress"]; contact.ContactEmail = adsViewAdverts["ContactEmail"]; contact.WebSite = adsViewAdverts["WebSite"]; db.AdsViewContacts.Add(contact); if (upload != null && upload.ContentLength > 0) { attachment.AdId = adId; attachment.Filename = Path.GetFileName(upload.FileName); attachment.FileType = FileType.Avatar; attachment.ContentType = upload.ContentType; string[] allowedExtentions = GetAllowedExtension(); foreach (var extention in allowedExtentions) { if (attachment.ContentType.Contains(extention.ToLower())) { using (var reader = new BinaryReader(upload.InputStream)) { attachment.Content = reader.ReadBytes(upload.ContentLength); db.AdsViewFile.Add(attachment); } break; } else { uploadedFileFailure = true; } } } ; db.SaveChanges(); string callbackUrl = SendEmailConfirmationTokenAsync(authUser, adId, "Confirm your ad."); return(RedirectToAction(nameof(CreateConfirmation), new { uploadedFile = uploadedFileFailure, contactInformation = contactInformationFailure })); //return RedirectToAction(nameof(CreateConfirmation), new RouteValueDictionary( new { controller = this, action = "CreateConfirmation", uploadedFile = uploadedFileFailure, contactInformation = contactInformationFailure })); } } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { ViewBag.Message("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage); } } } } return(RedirectToAction(nameof(Index))); }