using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using ModelStateError.Data; using ModelStateError.Models; namespace ModelStateError.Controllers; public class ContactsController : Controller { private readonly ModelStateErrorContext _context; public ContactsController(ModelStateErrorContext context) { _context = context; } // GET: Contacts public async Task Index() { return _context.Contact != null ? View(await _context.Contact.ToListAsync()) : Problem("Entity set 'ModelStateErrorContext.Contact' is null."); } // GET: Contacts/Details/5 public async Task Details(int? id) { if (id == null || _context.Contact == null) { return NotFound(); } var contact = await _context.Contact .FirstOrDefaultAsync(m => m.Id == id); if (contact == null) { return NotFound(); } return View(contact); } // GET: Contacts/Create public IActionResult Create() { return View(); } // POST: Contacts/Create // To protect from overposting attacks, 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,Name,ShortName,Email,PhoneNumber")] Contact contact) { // Attach Validation Error Message to the Model on validation failure. // if (contact.Name == contact.ShortName) { ModelState.AddModelError(nameof(contact.ShortName), "Short name can't be the same as Name."); } // if (_context.Contact.Any(i => i.PhoneNumber == contact.PhoneNumber)) { ModelState.AddModelError(nameof(contact.PhoneNumber), "The Phone number is already in use."); } if (_context.Contact.Any(i => i.Email == contact.Email)) { ModelState.AddModelError(nameof(contact.Email), "The Email is already in use."); } if (ModelState.IsValid) { _context.Add(contact); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(contact); } // // GET: Contacts/Edit/5 public async Task Edit(int? id) { if (id == null || _context.Contact == null) { return NotFound(); } var contact = await _context.Contact.FindAsync(id); if (contact == null) { return NotFound(); } return View(contact); } // POST: Contacts/Edit/5 // To protect from overposting attacks, 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,Name,ShortName,Email,PhoneNumber")] Contact contact) { if (id != contact.Id) { return NotFound(); } // Attach Validation Error Message to the Model on validation failure. if (_context.Contact.Any(i => i.PhoneNumber == contact.PhoneNumber)) { ModelState.AddModelError(nameof(contact.PhoneNumber), "The Phone number is already in use."); } if (_context.Contact.Any(i => i.Email == contact.Email)) { ModelState.AddModelError(nameof(contact.Email), "The Email is already in use."); } if (contact.Name == contact.ShortName) { ModelState.AddModelError(nameof(contact.ShortName), "Short name can't be the same as Name."); } if (ModelState.IsValid) { try { _context.Update(contact); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContactExists(contact.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(contact); } // // GET: Contacts/Delete/5 public async Task Delete(int? id) { if (id == null || _context.Contact == null) { return NotFound(); } var contact = await _context.Contact .FirstOrDefaultAsync(m => m.Id == id); if (contact == null) { return NotFound(); } return View(contact); } // // POST: Contacts/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { if (_context.Contact == null) { return Problem("Entity set 'ModelStateErrorContext.Contact' is null."); } var contact = await _context.Contact.FindAsync(id); if (contact != null) { _context.Contact.Remove(contact); } await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } // private bool ContactExists(int id) { return (_context.Contact?.Any(e => e.Id == id)).GetValueOrDefault(); } }