public async Task <ActionResult> Create([Bind(Include = "ImageLocation,id")] Photo photo) { if (ModelState.IsValid) { db.Photos.Add(photo); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(photo)); }
public async Task <ActionResult> Create(MessageCreateViewModel messageVM) { if (ModelState.IsValid) { var message = new SingleMessage { id = db.SingleMessages.Count(), MessageText = messageVM.MessageText, TimeOfMessage = DateTime.Now }; var userId = User.Identity.GetUserId(); var messageBoard = new MessageBoard { MessageID = db.MessageBoards.Count(), SingleMessage = message, RecieverID = messageVM.RecieverID, SenderID = userId, ProfileLinkerID = messageVM.LinkerId }; db.MessageBoards.Add(messageBoard); await db.SaveChangesAsync(); return(RedirectToAction("MessagesForUser", "MessageBoards", new { id = messageVM.RecieverID })); } return(View()); }
public async Task <ActionResult> Create([Bind(Include = "RentCost,Description,PhotoID,Street,City,Country,ZipCode,State")] Appartment appartment, HttpPostedFileBase photoInput) { bool isValid = ModelState.IsValid; if (isValid && photoInput != null && photoInput.ContentLength > 0) { if (!photoInput.ContentType.StartsWith("image/")) { isValid = false; ModelState.AddModelError("PhotoID", "Photo much be an image"); } } if (isValid) { if (photoInput != null && photoInput.ContentLength > 0) { var imageFolder = Server.MapPath("/UploadedImages"); var imageFileName = DateTime.Now.ToString("yyyy-MM-dd_HHmmss_fff") + "_" + photoInput.FileName; var imagePath = Path.Combine(imageFolder, imageFileName); photoInput.SaveAs(imagePath); var photo = new Photo() { ImageLocation = imageFileName }; appartment.Photo = photo; } var userId = User.Identity.GetUserId(); var appartmentOwner = db.AppartmentOwners.Find(userId); appartmentOwner.Appartment = appartment; db.Entry(appartmentOwner).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.PhotoID = new SelectList(db.Photos, "ID", "ImageLocation", appartment.PhotoID); return(View(appartment)); }