ASP.NET MVC Tricks (Remote Validation)

The necessity of validation a field in a form using AJAX it’s really common. Sometimes it can give a little work but the ASP.NET NET 3/4 has one attribute to help us for this kind of work.

Here is a simple demonstration of how to check if a username already exists in the system. Because the username should be unique.

Step 1

Include in the file Web.Config if the client validation and UnobtrusiveJavaScriptEnabled are enabled.

WebConfif

Step 2

Create an Action to validate if the username already exists.

Validation

Step 3

Put the attribute [Remote] in the field which we want to validate the model (in this case username field) and indicate which action and controller has the validation.

Attribute Remote

It’s already done, as you can see it’s very easy and effective.

Example: The username “User” is already in the system. (It’s not necessary to submit the form to validate if the user already exists)

After

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook