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 the 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 its 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