ASP NET core remote validation

preview_player
Показать описание
Remote validation in ASP.NET Core with an example.

What is remote validation in ASP.NET Core

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.

Text version of the video

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

Remote validation allows a controller action method to be called using client side script. This is very useful when you want to call a server side method without a full page post back.

Remote validation example

Checking, if the provided email is already taken by another user can only be done on the server. The following IsEmailInUse() controller action method checks if the provided email is in use.

[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public async Task[IActionResult] IsEmailInUse(string email)
{
var user = await userManager.FindByEmailAsync(email);

if (user == null)
{
return Json(true);
}
else
{
return Json($"Email {email} is already in use.");
}
}

This method should respond to both HTTP GET and POST. This is the reason we specified both the HTTP verbs (Get and Post) using [AcceptVerbs] attribute.
ASP.NET Core MVC uses jQuery remote() method which in turn issues an AJAX call to invoke the server side method.
The jQuery remote() method expects a JSON response, this is the reason we are returing JSON response from the server-side method (IsEmailInUse)

ASP.NET core remote attribute

The following is the model class for the User Registration View. Notice, we have decorated the Email property with the [Remote] attribute pointing it to the action method that should be invoked when the email value changes.

public class RegisterViewModel
{
[Required]
[EmailAddress]
[Remote(action: "IsEmailInUse", controller: "Account")]
public string Email { get; set; }

// Other properties
}

ASP.NET core remote validation not working

The following 3 client-side libararies are required in the order specified for the remote validation to work. If any of them are missing or not loaded in the order specified, remote validation will not work.

Рекомендации по теме
Комментарии
Автор

This is really nice I still remember back then we use to write lines and lines of code to perform such validation indeed this literally gave me nostalgia

NikhilShirgaonkar
Автор

Sir You are Great Greatest Teacher in the Whole Universe. Will Meet you one day That;s My Dream.

jattfauji
Автор

Thanks a ton for the remark at the end of your video about inject jQuery 🙏 Literally no other tutorials I researched have mentioned that and I was scratching my head to understand why 😂

rungxanh
Автор

Wow. Just getting to grips with the new .net core MVC offerings. As someone who used to hand craft javascript validation routines in notepad this is awesome stuff! Your explanations and examples are top notch! I'll be hitting up all of your stuff <3
My only query, is why is this an IActionResult instead of a bool... idk, guess I need to learn more.

russellgrice
Автор

Venkat, Thanks you for the all the lessons. Its really very impressive and the way you explaining the concept, its really excellent.

govindkrishnan
Автор

Nice explain so straight forward. Thanks Venkat

dhliu
Автор

Thanks for this lesson, I learned a lot from you!

ww
Автор

All please note: If you are using bootstrap 4.4.1 and the remote validation is not working, expand the notes at the top or see the blog page. In addition to jquery.js, you must install jquery-validate and jquery-validation-unobtrusive and reference them in _Layout.cshtml. Simplest way is to right click project name, Add, Client-Side Library and search for each and install them. These must appear before the bootstrap reference. If you are using bootstrap 4.4.1, you must use jquery 3.4.1.

javaguitarist
Автор

very useful lesson in asp.net thank you brother

stqlzoi
Автор

Hello Venkat, brilliant teaching again. So adding Remote attribute gets that input element make an AJAX call under the hood as the user types?

springfield-videos
Автор

Very good tutorial, thanks for your work!
Regards from portugal!

eduardocarvalho
Автор

I have feeling it is not good idea to expose method showing existence of user's account in the system. Hacker can do dictionary attack to find what emails are valid, then it will be easier to brute force password.
If you check for both email and pass at the same time - chances are less.

basilio
Автор

Dose anyone understand why/how IsEmailInUse() works BEFORE using the Remote attribute. There is nothing on the register button that is connected to isEmailInUse action or in any other way i could find so how dose the Jq Validation know this is the method i want to use?
Dose the Action intercept Post request ? why dosent every Action with a [httpPost] in Account get triggered then? I am very confused as to how Jq knows or whatever is happening.


Edit: I had some issues with this working with the Remote Attribute, thx to @Weicheng Chen for commenting about the Jquery(slim) cdn was causing the issue, use Jquery(min) cdn insted. Take a look at part 38(?) on how to change these.

martink
Автор

Can anyone explain how this works before the [remote] attribute is applied? As the only thing done at that point Is the creation of the "IsEmailInUse" action. How does the view know to use that controller for this particular field when posting the form?

gregoryfox
Автор

why we need to decorate the action method with both "Get" and "Post"? It works with just "Get" only. Did i miss anything.. Thanks! Great tutorials!

dilippatra
Автор

Good sir. Please make video on asp.net role management. How asp.net core differentiate admin and user login

mhraamirali
Автор

where we make that remote function affect registration only in the serverside before adding it to the class registrationviewmodel?

abanobrashdan
Автор

I'm wondering since The Account Controller should be accessed Anonymous why not decorate "AllowAnonymous" in the Controller level instead of Applying it to each Action method of the Controller???

abdifatahAnnaati
Автор

Hi Venkat. I have one query - when we click on Register Button, how the IsEmailInUse method is getting called? We have not mentioned any where in View to call this method. Could you please explain

chamantikka
Автор

Hello, thank you for these helpful courses. Actually, I think something going wrong with my project. I followed exactly the tutorial but it doesn't work. Actually, it doesn't even access to " IsMailInUse ". I don't know if the page is not loading the JQuery libraries or I am missing something. I copied and pasted the controller and the method name.

haythemkhedhiri
visit shbcf.ru