public ActionResult Admin() { var rolesAndProducts = new RoleProductViewModel { Products = _context.Products.ToList(), Roles = _context.Roles.ToList() }; return(View(rolesAndProducts)); }
public ActionResult AdminUploadDocument(RoleProductViewModel model) { try { for (int i = 0; i < Request.Files.Count; i++) { foreach (string organization in model.Organizations) { string productRepo = organization; HttpPostedFileBase file = Request.Files[i]; if (file != null && file.ContentLength > 0) { var originalDirectory = new DirectoryInfo(string.Format("{0}DocumentRepository", Server.MapPath(@"\"))); string pathString = Path.Combine(originalDirectory.ToString(), productRepo); var fileName1 = Path.GetFileName(file.FileName); if (!Directory.Exists(pathString)) { Directory.CreateDirectory(pathString); } var path = string.Format("{0}\\{1}", pathString, file.FileName); file.SaveAs(path); var newDocument = new Document() { DocumentID = GenerateId(), Name = fileName1, FilePath = path, ProductId = model.ProductId, LastModified = DateTime.Now.ToString(), }; if (model.ProductId == null) { newDocument.ProductId = 0; } _context.Documents.Add(newDocument); var newOrganization = new Organization { DocumentID = newDocument.DocumentID, Name = organization, FilePath = path }; _context.Organizations.Add(newOrganization); _context.SaveChanges(); } } } return(RedirectToAction("Admin", "Documents")); } catch (Exception ex) { Console.WriteLine(ex.Message); return(Json("Failed to upload document")); } }