Advertisement
reking12

Untitled

Apr 2nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using ToDoList.Data;
  6. using ToDoList.Models;
  7.  
  8. namespace ToDoList.Controllers
  9. {
  10.     public class TaskController : Controller
  11.     {
  12.         [HttpGet]
  13.         public IActionResult Index()
  14.         {
  15.             using (var db = new ToDoDbContext())
  16.             {
  17.                 var allTasks = db.Tasks.ToList();
  18.                 return View(allTasks);
  19.             }
  20.         }
  21.         [HttpGet]
  22.         public IActionResult Create()
  23.         {
  24.             return this.View();
  25.         }
  26.         [HttpPost]
  27.         public IActionResult Create(string title, string comments)
  28.         {
  29.             if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(comments))
  30.             {
  31.                 return RedirectToAction("Index");
  32.             }
  33.             Task task = new Task
  34.             {
  35.                 Title = title,
  36.                 Comments = comments
  37.             };
  38.             using (var db = new ToDoDbContext())
  39.             {
  40.                 db.Tasks.Add(task);
  41.                 db.SaveChanges();
  42.             }
  43.             return RedirectToAction("Index");
  44.         }
  45.         [HttpGet]
  46.         public IActionResult Edit(int id)
  47.         {
  48.             using (var db = new ToDoDbContext())
  49.             {
  50.                 var taskToEdit = db.Tasks.FirstOrDefault(t => t.Id == id);
  51.                 if (taskToEdit == null)
  52.                 {
  53.                     return RedirectToAction("Index");
  54.                 }
  55.                 return this.View(taskToEdit);
  56.             }
  57.         }
  58.         [HttpPost]
  59.         public IActionResult Edit(Task task)
  60.         {
  61.             if (!ModelState.IsValid)
  62.             {
  63.                 return RedirectToAction("Index");
  64.             }
  65.             using (var db = new ToDoDbContext())
  66.             {
  67.                 var taskToEdit = db.Tasks.FirstOrDefault(t => t.Id == task.Id);
  68.                 taskToEdit.Title = task.Title;
  69.                 taskToEdit.Comments = task.Comments;
  70.                 db.SaveChanges();
  71.             }
  72.             return RedirectToAction("Index");
  73.         }
  74.         [HttpGet]
  75.         public IActionResult Details(int id)
  76.         {
  77.             using (var db = new ToDoDbContext())
  78.             {
  79.                 Task taskDetails = db.Tasks.FirstOrDefault(t => t.Id == id);
  80.                 if (taskDetails == null)
  81.                 {
  82.                     RedirectToAction("Index");
  83.                 }
  84.                 return View(taskDetails);              
  85.             }
  86.         }
  87.         [HttpPost]
  88.         public IActionResult Delete(int id)
  89.         {
  90.             using (var db= new ToDoDbContext())
  91.             {
  92.                 var TaskToDelete = db.Tasks.FirstOrDefault(t => t.Id == id);
  93.                 if (TaskToDelete == null)
  94.                 {
  95.                     RedirectToAction("Index");
  96.                 }
  97.                 db.Tasks.Remove(TaskToDelete);
  98.                 db.SaveChanges();
  99.             }
  100.             return RedirectToAction("Index");
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement