/// <summary> /// View task for edit /// </summary> /// <param name="idTask">Identifier task</param> /// <returns></returns> public ActionResult Edit(Int32 idTask) { EditViewModel viewModel = new EditViewModel(); viewModel.Performers = _todoService.GetPerformerList().ToList(); viewModel.Statuses = _todoService.GetStatusList().ToList(); viewModel.Priorities = _todoService.GetPriorityList().ToList(); viewModel.Tags = _todoService.GetTagList().ToList(); Core.BusinessObjects.TaskDTO task = _todoService.GetTaskById(idTask); if (task == null) { ViewBag.Title = "Add task"; viewModel.Task = new Core.BusinessObjects.TaskDTO { CompletionDate = DateTime.Now.Date, CreationDate = DateTime.Now.Date, TaskId = 0, }; viewModel.TagsSelected = new List<Int32>(); } else { ViewBag.Title = "Edit task"; viewModel.Task = task; viewModel.TagsSelected = task.Tags.Select(x => x.TagId).ToList(); } viewModel.PathToImage = Url.Content(PathImages); return View(viewModel); }
public ActionResult Edit(EditViewModel model) { if (ModelState.IsValid) { TempData[MessageKey] = "Object has been saved. You can refresh this page without resend form data."; RedirectToAction("Edit"); } return RedirectToAction("Edit").WithModelState(); }
public ActionResult Edit(EditPostViewModel postViewModel) { String nameFieldWithImageName = "Task.ImageName"; String fullPath = String.Empty; String fileName = String.Empty; if (ModelState.IsValid) { HttpPostedFileBase postedFileBase = null; if (Request.Files.Count != 0 && Request.Files[0].FileName != String.Empty) { postedFileBase = Request.Files[0]; if (!AvailableExtensions.Any(x => x.Contains(Path.GetExtension(postedFileBase.FileName)))) { ModelState.AddModelError(nameFieldWithImageName, "File has invalid extension"); } else { fileName = String.Format("{0}{1}", Guid.NewGuid(), Path.GetExtension(postedFileBase.FileName)); fullPath = Path.Combine(Server.MapPath(PathImages), fileName); } } if (ModelState.IsValid) { try { if (postedFileBase != null) { if (postViewModel.Task.TaskId != 0) { String nameOldImage = _todoService.GetTaskById(postViewModel.Task.TaskId).ImageName; String oldFilePath = String.Format("{0}{1}", Server.MapPath(PathImages), nameOldImage); if (System.IO.File.Exists(oldFilePath)) { System.IO.File.Delete(oldFilePath); } } postedFileBase.SaveAs(fullPath); postViewModel.Task.ImageName = fileName; } else { //if Add new task without image - ImageName=null //else old imagename postViewModel.Task.ImageName = postViewModel.Task.TaskId == 0 ? null : _todoService.GetTaskById(postViewModel.Task.TaskId).ImageName; } if (postViewModel.Task.TaskId == 0) { _todoService.AddTask(postViewModel.Task, postViewModel.TaskTagsId); } else { _todoService.UpdateTask(postViewModel.Task, postViewModel.TaskTagsId); } return RedirectToAction("Index", new { state = "success" }); } catch (FileLoadException) { ModelState.AddModelError(nameFieldWithImageName, "File is not loaded"); } catch (DirectoryNotFoundException) { ModelState.AddModelError(nameFieldWithImageName, "DirectoryNotFound"); } } } EditViewModel viewModel = new EditViewModel(); viewModel.Performers = _todoService.GetPerformerList().ToList(); viewModel.Statuses = _todoService.GetStatusList().ToList(); viewModel.Priorities = _todoService.GetPriorityList().ToList(); viewModel.Tags = _todoService.GetTagList().ToList(); viewModel.Task = postViewModel.Task; viewModel.PathToImage = Url.Content(PathImages); viewModel.TagsSelected = postViewModel.TaskTagsId; return View(viewModel); }