Esempio n. 1
0
        public ActionResult Create([Bind(Include = "TicketId,Body,TicketAttachments")] TicketComment ticketComment, TicketAttachment ticketAttach, HttpPostedFileBase filePath)
        {
            if (ModelState.IsValid)
            {
                if (filePath != null)
                {
                    if (FileUploadHelper.IsWebFriendlyImage(filePath))
                    {
                        var fileName = Path.GetFileName(filePath.FileName);
                        fileName = fileName.Replace(' ', '_');
                        filePath.SaveAs(Path.Combine(Server.MapPath("~/Uploads/"), fileName));
                        ticketAttach.FilePath = "/Uploads/" + fileName; //assiging value of ticketAttach.FilePath to incomeing upload

                        ticketAttach.UploadDate = DateTime.Now;
                        ticketAttach.UserId     = User.Identity.GetUserId(); //property of this specific attachment is on left ...the right side of the = is what i am assigning to the property
                        db.Attachments.Add(ticketAttach);
                    }
                }


                ticketComment.CommentAttachment = ticketAttach.FilePath;     //ticketComment. COmmentAttachment = to the ticketAttach.FilePath
                ticketComment.OwnerId           = User.Identity.GetUserId(); //sending user id to DB
                ticketComment.CreatedDate       = DateTime.Now;              //sending current DATETIME to DB
                db.Comments.Add(ticketComment);
                db.SaveChanges();
                return(RedirectToAction("Details", "Tickets", new { id = ticketComment.TicketId })); // to go details page of whatever you want you need its ID.  Details action requires ID.  give it to it so it doesnt blow up.  object route value
            }


            ViewBag.OwnerId  = new SelectList(db.Users, "Id", "FirstName", ticketComment.OwnerId);
            ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketComment.TicketId);
            return(View(ticketComment));
        }