ASP.NET Core 2.0 MVC [Remote] Validation

Problem

How to implement model validation using [Remote] attribute in ASP.NET Core MVC.

Solution

In Startup configure middleware and services for MVC:

Add a model:

Add a controller:

Add a razor page and scripts for jQuery and its validation:

Discussion

ASP.NET Core MVC gives a useful [Remote] attribute to make AJAX calls to controller/action to perform server-side validation, without the full post-back. The attribute uses jQuery and it’s validation JavaScript files to perform the AJAX requests.

We simply annotate the model property with [Remote] attribute, specifying Controller and Action. The action itself returns a JSON result with either validation message or true.

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Mvc.ModelValidation.Remote

Leave a Reply