Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. using EmployeeData.Custom;
  5. using EmployeeData.Extensions;
  6. using EmployeeDomain;
  7. using EmployeeDomain.Custom;
  8.  
  9. namespace EmployeeWeb.Controllers
  10. {
  11.     public class WorkerController : Controller
  12.     {      
  13.         public WorkerController()
  14.             :this(new SqlUnitOfWork())
  15.         {
  16.            
  17.         }
  18.  
  19.         public WorkerController(IUnitOfWork unitOfWork)
  20.         {
  21.             _unitOfWork = unitOfWork;
  22.             _repository = _unitOfWork.Employees;
  23.         }        
  24.  
  25.         public ViewResult Index()
  26.         {
  27.             var model = _repository.FindAll()                                  
  28.                                    .OrderBy(e => e.HireDate);
  29.             return View(model);
  30.         }
  31.  
  32.  
  33.         public ViewResult Details(int id)
  34.         {
  35.             var model = _repository.FindById(id);
  36.  
  37.             return View(model);
  38.         }
  39.  
  40.        
  41.         public ViewResult Create()
  42.         {
  43.             return View();
  44.         }
  45.  
  46.  
  47.         [HttpPost]
  48.         public ActionResult Create([Bind(Exclude="Id")] Employee newEmployee)
  49.         {
  50.             if (ModelState.IsValid)
  51.             {
  52.                 _repository.Add(newEmployee);                
  53.                 _unitOfWork.Commit();
  54.                 return RedirectToAction("Index");
  55.             }
  56.  
  57.             return View(newEmployee);
  58.         }
  59.        
  60.      
  61.         public ViewResult Edit(int id)
  62.         {
  63.             var model = _repository.Find(e => e.Id == id)
  64.                                    .Single();
  65.             return View(model);
  66.         }
  67.        
  68.  
  69.         [HttpPost]
  70.         public ActionResult Edit(int id, FormCollection collection)
  71.         {
  72.             var model = _repository.FindById(id);
  73.             TryUpdateModel(model, new[] { "Name", "HireDate" });
  74.             if (ModelState.IsValid)
  75.             {
  76.                 _unitOfWork.Commit();
  77.                 return RedirectToAction("Index");
  78.             }
  79.  
  80.             return View(model);
  81.         }
  82.  
  83.         private readonly IUnitOfWork _unitOfWork;
  84.         private readonly IRepository<Employee> _repository;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement