public async Task <ActionResult> uploadAssignment(Study model, HttpPostedFileBase assignmentData = null) { string newAPath = ""; var student = _studentService.GetAll().FirstOrDefault(x => x.AppUserID == User.Identity.GetUserId()); if (VerifyAssignment(assignmentData)) { string AssignmentPath = "~/Assignment"; if (!Directory.Exists(Server.MapPath(AssignmentPath))) { Directory.CreateDirectory(Server.MapPath(AssignmentPath)); } var APath = Path.Combine(Server.MapPath(AssignmentPath), Guid.NewGuid().ToString() + " " + assignmentData.FileName); assignmentData.SaveAs(APath); APath = APath.Substring(APath.LastIndexOf("\\")); string[] spiltAPath = APath.Split('\\'); newAPath = spiltAPath[1]; newAPath = AssignmentPath + "/" + newAPath; var assignment = new Assignment { AssignmentUrl = newAPath, StudyID = model.ID, StudentRegNo = student.RegNo, SaveName = assignmentData.FileName, ContentType = assignmentData.ContentType, Score = null, //Time = DateTime.Now }; try { await _assignmentService.CreateAsync(assignment); TempData["success"] = "Assignment Uploaded successfully"; return(RedirectToAction("study", new { id = model.ID })); } catch (Exception) { System.IO.File.Delete(Server.MapPath(newAPath)); throw; } } else { ModelState.AddModelError("Doc", "Must be a document type"); } return(View(model)); }
public async Task <ActionResult> Create([Bind(Include = "Name,Materials,StartTime,EndTime,Note")] AssignmentCreateViewModel createdAssignment) { try { if (ModelState.IsValid) { IAssignment assignment = (Mapper.Map <Assignment>(createdAssignment)); await Service.CreateAsync(assignment); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again and if the problem persists see your system administrator."); } return(View(createdAssignment)); }