public ActionResult Create() { TaskModel model = new TaskModel(); if (TempData["TaskDate"] != null) model.TaskDate = (DateTime)TempData["TaskDate"]; else return RedirectToAction("Index"); ViewBag.TaskDate = model.TaskDate.ToString("ddd dd MMM yyyy"); return View(model); }
public ActionResult Create(TaskModel model) { if (ModelState.IsValid) { Action<int> executeTask = (int userTaskId) => { tb_Task task = new tb_Task { User_TaskId = userTaskId, ClientName = model.Client.Trim(), Description = model.Description.Trim(), Hours = model.HoursSpent, Type = model.Type }; _db.tb_Task.Add(task); _db.SaveChanges(); }; // get the user id and create the UserTask record var userId = User.Identity.GetUserId(); // check if task date for user has been created for the day var userTask = _db.tb_User_Task.Where(x => x.UserId == userId).ToList() .FirstOrDefault(x => x.TaskDate.Date == model.TaskDate.Date); if (userTask == null) { var userTaskObject = new tb_User_Task { UserId = userId, TaskDate = D(model.TaskDate).Date }; _db.tb_User_Task.Add(userTaskObject); _db.SaveChanges(); executeTask(userTaskObject.Id); } else { executeTask(userTask.Id); } return Search(D(model.TaskDate).ToString()); } return RedirectToAction("Index"); }
public ActionResult Edit(TaskModel model) { if (ModelState.IsValid) { tb_Task task = _db.tb_Task.FirstOrDefault(x => x.Id == model.Id); task.Type = model.Type.Trim(); task.ClientName = model.Client.Trim(); task.Description = model.Description.Trim(); task.Hours = model.HoursSpent; _db.SaveChanges(); return Search(D(model.TaskDate).ToString()); } return RedirectToAction("Index"); }
public ActionResult Edit(int id = 0) { if (id == 0) RedirectToAction("Index"); tb_Task task = _db.tb_Task.FirstOrDefault(x => x.Id == id); TaskModel model = new TaskModel { Id = task.Id, Client = task.ClientName.Trim(), Description = task.Description.Trim(), HoursSpent = task.Hours, Type = task.Type, TaskDate = task.tb_User_Task.TaskDate }; return View(model); }