move code snippets around (#129)
parent
75c749ddce
commit
2b54120292
|
@ -28,18 +28,24 @@ namespace ModelStateError
|
||||||
{
|
{
|
||||||
// Attach Validation Error Message to the Model on validation failure.
|
// Attach Validation Error Message to the Model on validation failure.
|
||||||
|
|
||||||
|
// <snippet_5>
|
||||||
|
if (Contact.Name == Contact.ShortName)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("Contact.ShortName",
|
||||||
|
"Short name can't be the same as Name.");
|
||||||
|
}
|
||||||
|
// </snippet_5>
|
||||||
|
|
||||||
if (_context.Contact.Any(i => i.PhoneNumber == Contact.PhoneNumber))
|
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))
|
if (_context.Contact.Any(i => i.Email == Contact.Email))
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("Contact.Email", "The Email is already in use.");
|
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 (!ModelState.IsValid || _context.Contact == null || Contact == null)
|
||||||
{
|
{
|
||||||
// if model is invalid, return the page with the model state errors.
|
// if model is invalid, return the page with the model state errors.
|
||||||
|
|
|
@ -55,19 +55,24 @@ public class ContactsController : Controller
|
||||||
public async Task<IActionResult> Create([Bind("Id,Name,ShortName,Email,PhoneNumber")] Contact contact)
|
public async Task<IActionResult> Create([Bind("Id,Name,ShortName,Email,PhoneNumber")] Contact contact)
|
||||||
{
|
{
|
||||||
// Attach Validation Error Message to the Model on validation failure.
|
// Attach Validation Error Message to the Model on validation failure.
|
||||||
|
// <snippet_5>
|
||||||
|
if (contact.Name == contact.ShortName)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError(nameof(contact.ShortName),
|
||||||
|
"Short name can't be the same as Name.");
|
||||||
|
}
|
||||||
|
// </snippet_5>
|
||||||
|
|
||||||
if (_context.Contact.Any(i => i.PhoneNumber == contact.PhoneNumber))
|
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))
|
if (_context.Contact.Any(i => i.Email == contact.Email))
|
||||||
{
|
{
|
||||||
ModelState.AddModelError(nameof(contact.Email), "The Email is already in use.");
|
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)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
_context.Add(contact);
|
_context.Add(contact);
|
||||||
|
|
Loading…
Reference in New Issue