Exemplo n.º 1
0
        public ActionResult Upload(int id)
        {
            var vm = new CustomerUploadVM();

            vm.CustomerId = id;

            return(View(vm));
        }
Exemplo n.º 2
0
        public ActionResult Upload(CustomerUploadVM vm)
        {
            var customer = _context.Customers.Include(c => c.CustomerFiles).SingleOrDefault(c => c.Id == vm.CustomerId);

            try
            {
                if (vm.File.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(vm.File.FileName);
                    var path     = Path.Combine(Server.MapPath("~/App_Data/CustomerFiles"), fileName);
                    vm.File.SaveAs(path);



                    var customerFile = new CustomerFile();

                    var userId = User.Identity.GetUserId();
                    var user   = _context.Users.SingleOrDefault(u => u.Id == userId);

                    customerFile.UploadedBy = user;
                    customerFile.DateTime   = DateTime.Now;
                    customerFile.Customer   = customer;
                    customerFile.FilePath   = path;
                    customerFile.FileName   = fileName;

                    _context.CustomerFiles.Add(customerFile);
                    customer.CustomerFiles.Add(customerFile);

                    _context.SaveChanges();
                }


                //ViewBag.Message = "Upload successful";

                //find customer by Id and add 'path' to files iCollection
                //return RedirectToAction("Index");


                return(RedirectToAction("CustomerFiles", new { id = customer.Id }));
            }
            catch
            {
                ViewBag.Message = "Upload failed";
                return(RedirectToAction("Uploads"));
            }
        }