Wednesday, August 31, 2016

Implementing Remote Validation using Data Annotations in ASP.NET MVC

If you want to implement Remote Validation using Data Annotations on your ViewModels in ASP.NET MVC, you can follow this article: https://msdn.microsoft.com/en-us/library/gg508808(vs.98).aspx

Even though this article references ASP.NET MVC 3, it still applies to ASP.NET MVC 5.

In order to ensure that you do not encounter conflicts with the System.ComponentModel.DataAnnotations namespace while using the Remote attribute, you should decorate your ViewModels with the fully qualified namespace for the attribute in this manner:

[System.Web.Mvc.Remote("IsUID_Available", "Validation")]

Then, once you have the necessary appSettings configured in your Web.config (these are set by default in most ASP.NET MVC projects), you should be able to leverage your MVC Controller Validation code as follows:



IMPOTANT NOTE: The parameter to the Remote validation method should MATCH the attribute name in the ViewModel, otherwise, you will end up getting a null value passed to your Remote validation method!

This will then perform the validation via an AJAX call seamlessly within your ASP.NET MVC Web Application!

No comments:

Post a Comment