Introduction
Most developers encounter a situation where it is necessary to check whether a user name already exists in the database before creating a new one, in other words we need to create a unique username in an application. All developers have a solution for this situation using a different approach but ASP.NET MVC has the feature of remote validation that solves such kinds of problems.
Remote validation makes an ajax request that could be either GET or POST, calls a method that has at least one parameter and returns a result in JSON format. The parameter value is the model's property value on which the remote validation is implemented and returns a JSON value that can be either false or true. If it is true then it's not valid otherwise it's valid.
I would like to recommend more articles on ASP.NET MVC validation so that you could have a strong knowledge in this area. Other articles are:
- ASP.NET MVC Server-Side Validation
- ASP.NET MVC Client-Side Validation
- ASP.Net MVC Validation Using Fluent Validation
- Fluent Validation in ASP.Net MVC
Using the Code
To implement remote validation in an application we have two scenarios, one is without an additional parameter and the other is with an additional parameter. First we create an example without an additional parameter. In this example we check whether a username exists or not. If the username exists then that means the input user name is not valid. We create a view model class "UserViewModel" under the Models folder and that code is:
- using System.Web.Mvc;
- namespace RemoteValidation.Models
- {
- public class UserViewModel
- {
- public string UserName
- {
- get;
- set;
- }
- public string Email
- {
- get;
- set;
- }
- }
- }
Now we create a static data source, in other words we create a static list of UserViewModel in which we could check whether a username exists or not. You can also use the database rather than a static list. The following code snippet is for StaticData.
- using RemoteValidation.Models;
- using System.Collections.Generic;
- namespace RemoteValidation.Code
- {
- public static class StaticData
- {
- public static List < UserViewModel > UserList
- {
- get {
- return new List < UserViewModel >
- {
- new UserViewModel
- {
- UserName = "Sandeep", Email = "[email protected]"
- },
- new UserViewModel
- {
- UserName = "Raviendra", Email = "[email protected]"
- }
- }
- }
- }
- }
- }
- using RemoteValidation.Code;
- using System.Linq;
- using System.Web.Mvc;
- namespace RemoteValidation.Controllers
- {
- public class ValidationController: Controller
- {
- [HttpGet]
- public JsonResult IsUserNameExist(string userName)
- {
- bool isExist = StaticData.UserList.Where(u = > u.UserName.ToLowerInvariant().Equals(userName.ToLower())).FirstOrDefault() != null;
- return Json(!isExist, JsonRequestBehavior.AllowGet);
- }
- }
- }
- using System.Web.Mvc;
- namespace RemoteValidation.Models
- {
- public class UserViewModel
- {
- [Remote("IsUserNameExist", "Validation", ErrorMessage = "User name already exist")]
- public string UserName
- {
- get;
- set;
- }
- public string Email
- {
- get;
- set;
- }
- }
- }
- using RemoteValidation.Models;
- using System.Web.Mvc;
- namespace RemoteValidation.Controllers
- {
- public class UserController: Controller
- {
- [HttpGet]
- public ActionResult AddUser()
- {
- UserViewModel model = new UserViewModel();
- return View(model);
- }
- }
- }



Aditya GopuPosted Sep 30, 2021, 8:22 AM
Thank you for the post. I want to use this Remote decorator in Create view but not in Update view, is there any way other than separate view models.
IMAD AYOUBPosted Mar 30, 2021, 10:36 AM
Excellent and useful tutorial. Thanks a lot.
A-aziz MahdiPosted Apr 26, 2020, 8:53 AM
Thank you Very Much, That was a very helpful and very clear explanation
Shailendra Kumar SinghPosted Sep 9, 2019, 11:21 AM
An easy way of explaining that.
Karen TonoyanPosted Nov 2, 2016, 2:35 AM
Very Useful article...
SubashPosted Oct 30, 2016, 12:23 PM
Nice useful one
Manav PandyaPosted Oct 30, 2016, 4:13 AM
Nice share sir ...
Thiruppathi RPosted Oct 29, 2016, 4:54 AM
simple way to used entity, nice..