Remote validation is used to make server calls to validate data without posting the entire form to the server when server side validation is preferable to the client side. It's all done by setting up a model and controller which is pretty neat.
Suppose if any field in a database table must be unique, and we want to check this uniqueness on the client side (after text change of text-boxes) instead of posting the whole page.
Problem Statement


How to apply validation using Remove Attribute to resolve this issue?
When a user provides a user name which already exists, the associated validation error message should be displayed as below,

Create a table
- CREATE TABLE [dbo].[User] (
- [Id] INT NOT NULL,
- [UserName] VARCHAR (50) NOT NULL,
- [Password] NCHAR (10) NOT NULL,
- PRIMARY KEY CLUSTERED ([Id] ASC)
- );
Create an empty MVC project, and then add an ado.net Entity model using the table User. Then build the solution.
Add UsersController with following settings,
- ControllerName : UsersController
- Template

- Model Class: User

- Data Context Class: TestEntities
- View: Razor
Copy and paste the below function into UsersController. This method will be used to perform the validation. An ajax request is triggered by this method. If this method returns true, validation succeeds, else validation failed and the form is prevented from being submitted. The parameter name (UserName) should match the field name on the view.
- public JsonResult IsUserNameAvailable(string UserName)
- {
- return Json(!db.Users.Any(u => u.UserName == UserName), JsonRequestBehavior.AllowGet);
- }
Add another class under the model folder and copy paste the below code.

IMAD AYOUBPosted Mar 29, 2021, 10:42 PM
Thanks for the article. Is it possible without EF? I am using stored procedures. Thanks
Ching WangPosted Aug 13, 2020, 10:21 PM
After valid success, I submit the form, but it's not post to controller, and I don't know why?
Prasad PinnaduwagePosted Jul 24, 2020, 8:56 AM
I downloaded your code, added DB connection, created the user table and checked. it didn't work. ie. the code didn't hit IsUserNameAvailable method. Am I missing anything
Vikash KumarPosted Aug 19, 2019, 6:08 AM
How to skip Remote validation on update
Arturo CarmonaPosted May 21, 2019, 11:08 AM
Excelente art?culo, bien explicado
Laxmidhar SahooPosted Apr 19, 2019, 10:42 AM
A good article Thanks