1

Whatever tutorial or sample I use I get the exact same message over and over :

enter image description here

My Model :

public class Login
{
    [StringLength(50)]
    [Remote("CheckUserName", "Index", ErrorMessage = "Username already exists")]
    public string UserName { get; set; }
}

My Controller :

[HttpPost][HttpGet]
    public JsonResult CheckUserName(string userName)
    {
        var repository = new List<Login>
                         {
                             new Login { UserName = "Gareth" },
                             new Login { UserName = "Lionel" }
                         };

        var exists = repository.Exists(x => x.UserName.Equals(userName));

        return exists ? Json(true) : Json($"Username already exists!");
    }

My View :

    <form asp-controller="Home" asp-action="Save" method="post">
    <div class="form-group">
        <label asp-for="UserName" class="control-label"> </label>
        <input asp-for="UserName" class="form-control" />
        <span asp-validation-for="UserName" class="text-danger"> </span>
    </div>

    <div class="form-group">
        <input type="submit" value="Save " class="btn btn-default" />
    </div>
</form>

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

What did I forget or do wrong?

Using ASP .NET Core 2.2 MVC project.

  • What's the name of your controller class? – ESG Nov 30 '20 at 14:58
  • @ESG CheckUserName with returnType JsonResult – user11208424 Nov 30 '20 at 15:07
  • @user11208424 this is the validation method, ESG is asking about the class, e.g. the controller. (that should inherit from Controller) – Pac0 Nov 30 '20 at 15:34
  • Ah its just the HomeController – user11208424 Nov 30 '20 at 15:45
  • I do exactly what I find in the samples and even this sample https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0 But I keep getting the "No URL error message".... – user11208424 Nov 30 '20 at 15:45
  • 2
    @user11208424 try this if Home is controller: `[Remote("CheckUserName", "Home", ErrorMessage = "Username already exists")]` See default constructor of Remote [here](https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.remoteattribute.-ctor?view=aspnet-mvc-5.2#System_Web_Mvc_RemoteAttribute__ctor_System_String_System_String_System_String_) – Pirate Nov 30 '20 at 15:48
  • @Dave Lel that was it, thx everyone! – user11208424 Nov 30 '20 at 15:52

0 Answers0