Exemplo n.º 1
0
        //  [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = @"TransportId,DepartureDateTime,DepartureTime,ArrivalDateTime,ArrivalTime,
                                            DepartureAddress,ArrivalAddress")] Transport transport, int?driver, int?transcomp)
        {
            if (ModelState.IsValid)
            {
                EmployeeDAL ed  = new EmployeeDAL();
                var         drv = ed.GetEmployeeById(Convert.ToInt32(driver));
                if (drv != null)
                {
                    transport.Driver = drv;
                }

                var trans = ed.GetTransportCompanyById(Convert.ToInt32(transcomp));//transport.TransportCompId);
                if (trans != null)
                {
                    transport.TransportComp = trans;
                }

                ed.AddTransportAndSaveChanges(transport);

                string             path       = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tickets");
                string             ticketPath = null;
                HttpPostedFileBase file       = null;
                if (Request.Files.Count > 0)
                {// preluam fisierul din lista de fisiere
                    file = Request.Files[0];
                    // construim path relativ la poza
                    ticketPath = Path.Combine(path, file.FileName);
                    // generam numele fisierului de pe disk unde vom salva poza
                    string filePath = transport.TransportId + Path.GetExtension(file.FileName);//path - ul care va fi folosit pentru afisarea imaginii
                    //c:\...\tickets\1.pdf
                    string newTicketPath = Path.Combine(path, transport.TransportId + Path.GetExtension(file.FileName));
                    //salvam fisierul in tickets
                    file.SaveAs(newTicketPath);
                    //salvam calea catre pdf in baza de date
                    transport.PlaneTicketPath = filePath;
                    //salvam modificarile
                    ed.SaveChanges();
                    Session["tId"] = transport.TransportId;
                }

                int    id     = Convert.ToInt32(Session["reqId"]);
                string retUrl = "../Request/EditHR/" + id.ToString();
                return(RedirectToAction(retUrl));
            }

            ViewBag.DepartureAddressId = new SelectList(db.MyAddress, "AddressId", "DepartureAddress", transport.DepartureAddress);
            ViewBag.ArrivalAddressId   = new SelectList(db.MyAddress, "AddressId", "ArrivalAddress", transport.ArrivalAddress);
            ViewBag.DrvId       = new SelectList(db.MyEmployee, "EmployeeId", "FullName", transport.DriverId);
            ViewBag.TransCompId = new SelectList(db.TransportCompanies, "TransportCompanyId", "CompanyName", transport.TransportCompId);


            return(View(transport));
        }