Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using remote validation with ASP.NET Core

I am trying to create a remote validation as follows:

[Remote("CheckValue", "Validate", ErrorMessage="Value is not valid")]
public string Value { get; set; }

I am using ASP.NET Core (ASP.NET 5) and it seems Remote is not available. Does anyone know how to do this with ASP.NET CORE?

like image 280
Miguel Moura Avatar asked Mar 16 '16 10:03

Miguel Moura


1 Answers

The RemoteAttribute is part of ASP.Net MVC Core:

  • If you are using RC1, it is in the Microsoft.AspNet.Mvc namespace. See RemoteAttribute in github.
  • After the renaming planned in RC2, it will be in the Microsoft.AspNetCore.Mvc namespace. See RemoteAttribute in github.

For example, in RC1 create a new MVC site with authentication. Then update the generated LoginViewModel with a dummy remote validation calling a method in the home controller:

using Microsoft.AspNet.Mvc;
using System.ComponentModel.DataAnnotations;
public class LoginViewModel
{
    [Required]
    [EmailAddress]
    [Remote("Foo", "Home", ErrorMessage = "Remote validation is working")]
    public string Email { get; set; }

    ...
}

If you create that dummy method in the home controller and set a breakpoint, you will see it is hit whenever you change the email in the login form:

public class HomeController : Controller
{

    ...

    public bool Foo()
    {
        return false;
    }
}
like image 160
Daniel J.G. Avatar answered Oct 21 '22 14:10

Daniel J.G.