public ActionResult Create([Bind(Include = "TicketId")] TicketAttachment ticketAttachment, string attachmentTitle, string attachmentDescription, HttpPostedFileBase attachment) { if (attachment == null) { return(RedirectToAction("Details", "Tickets", new { id = ticketAttachment.TicketId })); } if (ModelState.IsValid) { ticketAttachment.Title = attachmentTitle; ticketAttachment.Description = attachmentDescription; ticketAttachment.Created = DateTime.Now; ticketAttachment.UserId = User.Identity.GetUserId(); if (ImageUploadValidator.IsValidAttachment(attachment)) { var fileName = Path.GetFileName(attachment.FileName); attachment.SaveAs(Path.Combine(Server.MapPath("~/Uploads/"), fileName)); ticketAttachment.FileUrl = "/Uploads/" + fileName; } db.TicketAttachments.Add(ticketAttachment); db.SaveChanges(); return(RedirectToAction("Details", "Tickets", new { id = ticketAttachment.TicketId })); } return(View(ticketAttachment)); }
public ActionResult Create([Bind(Include = "TicketId")] TicketAttachment ticketAttachment, string attachmentTitle, string attachmentDescription, HttpPostedFileBase FilePath) { if (ModelState.IsValid) { ticketAttachment.Title = attachmentTitle; ticketAttachment.Description = attachmentDescription; ticketAttachment.Created = DateTime.Now; ticketAttachment.UserId = User.Identity.GetUserId(); //using my imageHelpers to decide if it is good file or not if (ImageUploadValidator.IsValidAttachment(FilePath)) { var fileName = Path.GetFileName(FilePath.FileName); FilePath.SaveAs(Path.Combine(Server.MapPath("~/Attachments/"), fileName)); ticketAttachment.FilePath = "/Attachments/" + fileName; } db.TicketAttachments.Add(ticketAttachment); db.SaveChanges(); //return RedirectToAction("Index"); return(RedirectToAction("Dashboard", "Tickets", new { id = ticketAttachment.TicketId })); } ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketAttachment.TicketId); ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", ticketAttachment.UserId); return(View(ticketAttachment)); }