Marimba Marimba - 1 year ago 110
ASP.NET (C#) Question

Show only one Register error instead of two

I have an ASP.Net MVC site that uses ASP.Net Identity for managing users.

I use the default mechanism that the Email of the user is also his username. When a user wants to register with an Email which is already taken two error messages are shown:

  • Name is already taken.

  • Email '' is already taken.

The structure of the Register form:

How the Register form looks like

HttpPost Register Method of AccountController:

public async Task<ActionResult> Register(RegisterViewModel model)
if (ModelState.IsValid)
var user = new ApplicationUser {UserName = model.Email, Email = model.Email};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
string callbackUrl =
await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");

return View("Info");

// If we got this far, something failed, redisplay form
return View(model);

How can I only show the Email Error message to the user?

Answer Source

Replace your AddErrors() method with

ModelState.AddModelError("", String.Format("{0} is already taken", model.Email))

or, if you have included @Html.ValidationMessageFor(m => m.Email) in the view and want the message to be associated with the form control, rather than in the summary

ModelState.AddModelError("Email", String.Format("{0} is already taken", model.Email))

Side note: You might also want to consider using a RemoteAttribute so that you get client side validation before submitting the form - refer How to: Implement Remote Validation in ASP.NET MVC (although you should also keep the code above in (the rare) case 2 users submit the same Email at the same time).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download