using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using DMSSample.Models; using DMSSample.Services; using Microsoft.AspNetCore.Mvc.Filters; namespace DMSSample.Controllers { public class PersonController : BaseController { public PersonController(IDbContextService dbContextService) : base(dbContextService){ } public IActionResult Index(string searchString, bool? isPopup){ ViewData["SearchString"] = searchString; if(isPopup == null){ return View(); }else{ return View("Popup"); } } // GET: Person public async Task SearchList(string sortOrder, string searchString, int? page, bool? isPopup) { var person = from p in _context.Person select p; ViewData["SortOrder"] = sortOrder; ViewData["SearchString"] = searchString; if(!String.IsNullOrEmpty(searchString)){ person = person.Where(p => p.FullName.StartsWith(searchString)); } switch(sortOrder){ case "full_name_desc": person = person.OrderByDescending(p => p.FullName); break; case "full_name": person = person.OrderBy(p => p.FullName); break; case "first_name_desc": person = person.OrderByDescending(p => p.FirstName); break; case "first_name": person = person.OrderBy(p => p.FirstName); break; case "last_name_desc": person = person.OrderByDescending(p => p.LastName); break; case "last_name": person = person.OrderByDescending(p => p.LastName); break; default: person = person.OrderBy(p => p.FullName); break; } if(isPopup == null){ int pageSize = 10; return PartialView("SearchList", await PaginatedList.CreateAsync(person.AsNoTracking(), page ?? 1, pageSize)); }else{ int pageSize = 10; return PartialView("PopupList", await PaginatedList.CreateAsync(person.AsNoTracking(), page ?? 1, pageSize)); } } // GET: Person/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var person = await _context.Person .SingleOrDefaultAsync(m => m.Id == id); if (person == null) { return NotFound(); } return View(person); } // GET: Person/Create public IActionResult Create() { return View(); } // POST: Person/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Id,FullName,LastName,FirstName")] Person person) { if (ModelState.IsValid) { _context.Add(person); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(person); } // GET: Person/Edit/5 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var person = await _context.Person.SingleOrDefaultAsync(m => m.Id == id); if (person == null) { return NotFound(); } return View(person); } // POST: Person/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, [Bind("Id,FullName,LastName,FirstName")] Person person) { if (id != person.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(person); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(person.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(person); } // GET: Person/Delete/5 public async Task Delete(int? id) { if (id == null) { return NotFound(); } var person = await _context.Person .SingleOrDefaultAsync(m => m.Id == id); if (person == null) { return NotFound(); } return View(person); } // POST: Person/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { var person = await _context.Person.SingleOrDefaultAsync(m => m.Id == id); _context.Person.Remove(person); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool PersonExists(int id) { return _context.Person.Any(e => e.Id == id); } } }