public void PostAsync(Chat chat) { if (ModelState.IsValid) { chat.UserId = int.Parse(User.FindFirst("UserId").Value); chat.Date = DateTime.Now; _context.Add(chat); _context.SaveChanges(); } return; }
public IActionResult TodoView(Todo todo, int id, int delete) { //IDが0なら登録処理 if (id == 0) { if (todo.TodoContent == "") { return(null); } todo.UserId = int.Parse(User.FindFirst("UserId").Value); todo.AddDate = DateTime.Now; _context.Add(todo); _context.SaveChanges(); return(ViewComponent("TodoView", todo.UserId)); } //Deleteに値があれば削除処理 if (delete != 0) { _context.Todo.Remove(_context.Todo.Where(m => m.TodoId == delete).FirstOrDefault()); _context.SaveChanges(); return(ViewComponent("TodoView", int.Parse(User.FindFirst("UserId").Value))); } //更新処理 var tmp = _context.Todo.Where(m => m.TodoId == id).FirstOrDefault(); if (tmp.Complete == true) { tmp.Complete = false; } else { tmp.Complete = true; } _context.SaveChanges(); return(ViewComponent("TodoView", tmp.UserId)); }