In a real time web application the user registration page has a username field where the user must specify a unique username. Once a user provides a username, then to check if the field is unique, a call to the database must be made. If the field is not unique a validation error message is displayed to the front-end user such as “UserName already in use”.
Let's look at it practically.
DEMO
Step 1
The first step is to create a table where we will store the details.
- CREATE DATABASE db_UniqueUserName
- USE db_UniqueUserName
- CREATE DATABASE db_UniqueUserName
- USE db_UniqueUserName
- SELECT * FROM tblUser

Step 2
The next step is to create an MVC 5 web application.

Click OK.
Choose template as empty and select MVC as the project type.

Click OK.
Step 3
The next step is to add an ADO.Net Entity Data Model in the Models folder.

Provide this Model a name and click OK.

Choose EF Designer from database.

Click Next.
Add a new connection by clicking on the New Connection button.

Click OK.
Provide the connection settings a name.

Click Next.
Choose the required table.

Click Finish.
Step 4
Change the entity name from tblUser to User.

Step 5
The next step is to add a Controller.

Select MVC 5 Controller with views, using Entity Framework.

Click Add.
Note
This template will add all the required action methods, views and the required HTML codes for us.
Choose the Model class : User
Choose Data Context class :UserDbContext

Provide the controller a name.
Click Add
Run the application and click the Create New link.
Specify the user name and password.
Click Create.


So, a record has been added to our database.
Now add a new record and specify the same user name again and click create.
Look at the records we have. We have two records with the same user name. But in reality two users cannot have the same user name.

Now let's see how to solve this problem.
To solve this problem we can use RemoteAttribute.
Step 1
In the HomeController create a method and for that write the following.
- public JsonResult IsUserExists(string UserName)
- {
- //check if any of the UserName matches the UserName specified in the Parameter using the ANY extension method.
- return Json(!db.Users.Any(x => x.UserName == UserName) ,JsonRequestBehavior.AllowGet);
- }
Step 2
The next step is to hook this method up with the username property and for that first we need to add a class file in the models folder, add a partial User class and provide the required customization to the UserName property.
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- namespace UniqueField.Models
- {
- [MetadataType(typeof(UserMetaData))]
- public partial class User
- {
- }
- class UserMetaData
- {
- [Remote("IsUserExists","Home",ErrorMessage="User Name already in use")]
- public string UserName { get; set; }
- }
- }
In create.cshtml we need to specify the source of the three jQuery files in the given order.
- <h2>Create</h2>
- <script src="~/Scripts/jquery-1.10.2.js"></script>
- <script src="~/Scripts/jquery.validate.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
Specify the same user name.
Without clicking the create button, we got this validation error message.

But if we specify a different username that has not been taken yet, it will not provide us the validation error message.

I hope you find this helpful.
Thank you for reading.

Hilmi DönmezPosted Aug 28, 2018, 4:19 PM
Very thank you :)
Vishal DongaPosted Jul 2, 2018, 12:23 AM
JsonRequestBehavior.AllowGet is not working now.
RaGy AtefPosted Jun 11, 2018, 11:11 AM
to handle edit String idS = Session["id"].ToString(); if (!string.IsNullOrEmpty(idS )) { int id= int.Parse(idS ); return Json(!db.users.Any(x => x.name== name&& x.id!= id), JsonRequestBehavior.AllowGet); } return Json(!db.users.Any(x => x.name== name), JsonRequestBehavior.AllowGet);
Aniket NarvankarPosted Jun 7, 2018, 7:11 AM
How to handle validation in edit?
Subramonian InianPosted Apr 6, 2018, 12:55 AM
Im getting an error "The name db does not exist in the current context" pls help
Sally PepinPosted Oct 27, 2017, 3:29 PM
Does not work for me.
Shawn GamblePosted Jun 14, 2017, 3:32 PM
I am having a few issues trying to replicate what you are doing here. It is likely because I am very new to this, I have project just like this but I can't seem to make it work right. any way you might be able to see what I am doing wrong ?
Nilesh MakavanaPosted Mar 10, 2017, 10:32 PM
How to manage this validation in edit
barathi rajaPosted Aug 7, 2016, 7:32 AM
Nice Article .. HTML Page Send me bro
Hewbhurt GabonPosted Aug 11, 2015, 9:31 AM
A straight forward no bs solution. Thank you so much!
hardik patelPosted Apr 30, 2015, 7:05 AM
nice work thanks.....
Karthik Muthu KaruppanPosted Apr 24, 2015, 10:55 AM
good
NitinPosted Apr 24, 2015, 9:53 AM
Good one
Manoj KulkarniPosted Apr 24, 2015, 2:14 AM
Nice article. Thank you for sharing
Gowtham RajamanickamPosted Apr 24, 2015, 12:41 AM
good
Santhakumar MunuswamyPosted Apr 23, 2015, 11:23 PM
Thanks for nice article