From 2b54120292720465f09ed8bca8603801ad272d8e Mon Sep 17 00:00:00 2001
From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
Date: Thu, 2 Mar 2023 14:27:19 -1000
Subject: [PATCH] move code snippets around (#129)
---
.../Pages/Contacts/Create.cshtml.cs | 16 +++++++++++-----
.../Controllers/ContactsController.cs | 15 ++++++++++-----
2 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs b/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs
index 4acf916..d2cc124 100644
--- a/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs
+++ b/RazorPages/models/ModelStateError/Pages/Contacts/Create.cshtml.cs
@@ -28,18 +28,24 @@ namespace ModelStateError
{
// Attach Validation Error Message to the Model on validation failure.
+ //
+ if (Contact.Name == Contact.ShortName)
+ {
+ ModelState.AddModelError("Contact.ShortName",
+ "Short name can't be the same as Name.");
+ }
+ //
+
if (_context.Contact.Any(i => i.PhoneNumber == Contact.PhoneNumber))
{
- ModelState.AddModelError("Contact.PhoneNumber", "The Phone number is already in use.");
+ ModelState.AddModelError("Contact.PhoneNumber",
+ "The Phone number is already in use.");
}
if (_context.Contact.Any(i => i.Email == Contact.Email))
{
ModelState.AddModelError("Contact.Email", "The Email is already in use.");
}
- if (Contact.Name == Contact.ShortName)
- {
- ModelState.AddModelError("Contact.ShortName", "Short name can't be the same as Name.");
- }
+
if (!ModelState.IsValid || _context.Contact == null || Contact == null)
{
// if model is invalid, return the page with the model state errors.
diff --git a/mvc/models/ModelStateError/Controllers/ContactsController.cs b/mvc/models/ModelStateError/Controllers/ContactsController.cs
index 39a0e86..01e5717 100644
--- a/mvc/models/ModelStateError/Controllers/ContactsController.cs
+++ b/mvc/models/ModelStateError/Controllers/ContactsController.cs
@@ -55,19 +55,24 @@ public class ContactsController : Controller
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.");
+ 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)
{
_context.Add(contact);