Remote Attribute - MVC

May 04, 2019 BY xtreme

A mechanism used to make remote server call to validate specific data without posting the entire form to 
the server. For example a registration form, having a username textbox and while entering username, we
want to check whether that username already exists in database or not without posting the complete form.

    [Remote("Action name", "Controller", ErrorMessage = "Error Message")]

    public partial class EmployeeDetail{
        public int EmpID { get; set; }            
        [Remote("CheckUserNameExists", "Register", ErrorMessage = "UserName already exists!")]
        public string UserName { get; set; }
        public string Name { get; set; }

    public ActionResult CheckUserNameExists(string Username){
        bool IsExists = false;        

        try    {
            using (var dbcontext = newEmployeeDBEntities())
                var record = from r in dbcontext.Employees
                    where r.Name.Equals(Username.Trim())
                    select r;

                if (nameexits.Count() > 0)    
                    IsExists = true;                    
            return Json(!IsExists, JsonRequestBehavior.AllowGet);
        catch (Exception){
            return Json(false, JsonRequestBehavior.AllowGet);

    <div class="editor-field">
        @Html.TextBoxFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)