Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Remote Validation in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 309
    Comment on it

    Hi Friends

    Whenever we register a user in our registration form we need to check for existence of current email/user name or any unique identification property. You certainly don't want two users to be registered with same email Ids. And you also don't want to make a postback  just for checking a user. So we need to do that asynchronously.

    Now that "asynchronously" word rings bells as that includes that haunting jQuery Ajax syntax.(At least I found that haunting as I was stuck several times in those silly comma issues:-)).

     

    Don't worry, you don't need to write a single line of those jQery.Ajax calls and headbutting with json's comma and quotes errors. ASP.Net MVC provides a very neat and easy solution for the same. You just need to define a "Remote" attribute and a method of json type. Let's take a look.

     

    Step 1:- Define the attribute "Remote" for the property like Email or Username.

    In my case that was email.So we'll define it like following.

    [Remote("EmailExists", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]
    public string email { get; set; }

    The "Remote" attribute comes from "System.Web.Mvc" . It may look like a data annotation but it's not. You can always write "Remote" and resolve the rest of the things by rightcllick-> resolve.

     

    We see that the "Remote" attribute has some parameters. Let's take a closer look at them:-

     

    "EmailExists":- This is the name of the method that is going to check for the existence of user by Username or EmailId. You can write a better name for sure.

     

    "Account":-This is the controller in which the above method resides.

     

    HttpMethod:- I don't need to explain I think. All that stuff get..post ..you know..

     

    ErrorMessage:- This is the message you show to your user when he enters a duplicate email or username. Go ahead,create your own and that should be scary enough to make your user run away from registering.:-). Just kidding, change this message as per your need.

     

    Step 2:- Define the Method

    Now we need to define the method where the actual check happens. So here we go:-
     

    public JsonResult EmailExists(string email)
      {
       bool exists=false;
       exists=myRepository.FindbyMail(email);//Some method returning bool type from repository. If you are using an ORM like entity, you can use "Any" for the same. Just for understanding the purpose of the method.
       return Json(!exists, JsonRequestBehavior.AllowGet);
    
     }

    Now just put your method call and store your bool result in the "exists" variable. You can change your variable too and that's up to you. But do use the same method name for the attribute parameter and method name.

     

    So you've got a easier method for checking the user existence. Use it in your code and please ask your doubts in the comments section.

    Happy Coding:-)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: