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);