public ActionResult Create(CreateViewModel createViewModel) { if (!ModelState.IsValid) { return View(createViewModel); } var selectedDocumentCategory = DocumentCategoryService.FindById(createViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory); var document = new Document() { Name = createViewModel.Name, Description = createViewModel.Description, DocumentCategoryId = createViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory != -1 ? (int?)createViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory : null, DocumentCategory = selectedDocumentCategory }; foreach (string file in Request.Files) { if (Request.Files[file].ContentLength <= 0 || Request.Files[file] == null) continue; string pathToSave = Server.MapPath("~/Files/Documents"); string oldFilename = Path.GetFileName(Request.Files[file].FileName); string fileExtension = Path.GetExtension(Request.Files[file].FileName); string uploadFileName = String.Format(Guid.NewGuid() + fileExtension); Request.Files[file].SaveAs(Path.Combine(pathToSave, uploadFileName)); document.OldFileName = Request.Files[file].FileName; document.UploadFileUrl = Path.Combine(pathToSave, uploadFileName); } DocumentService.CreateDocument(document); TempData["DocumentId"] = document.DocumentId; TempData["DocumentName"] = document.Name; TempData["Message"] = DocumentsMessage.CreateSuccess; return RedirectToAction("index", "documents"); }
public ActionResult Create() { var dropdownlist_DocumentCategory_Property = new CreateViewModel.DropDownList_DocumentCategory() { DocumentCategories = DocumentCategoryService.GetDocumentCategories().ToList() }; var createViewModel = new CreateViewModel() { DropDownList_DocumentCategory_Property = dropdownlist_DocumentCategory_Property }; return View(createViewModel); }