Search code examples
c#asp.net-coreremote-validation

ASP.NET Core Remote Validation not showing


I tried adding some simple Remote validation to my webpage (ASP.NET Core 2.2, MVC), but it does not show on GUI.

Code from my Client (view) model:

[StringLength(32, ErrorMessage = "Max input is 32 characters.")]
[Remote(action: "CheckTaxNumber", controller: "Clients")]
public string TaxNumber { get; set; }

Code from my View:

<div class="form-group">
   <label>Tax No.: </label>
   <input class="form-control" asp-for="TaxNumber" maxlength="32" />
   <span asp-validation-for="TaxNumber"></span>
</div>

Code from my Controller:

[AcceptVerbs("Get", "Post")] 
public IActionResult CheckTaxNumber(string taxNumber)
{
   var check = _clients.CheckTaxNumber(taxNumber);

   if(check.Result)
   {
      return Json(true); 
   }
   else
   {
      return Json(check.ErrorMessage);
   }
}

After the user changes the input field (for the tax numbe) the validation fires and returns a result (true/false + error message), but it does not show on the form.

How should I fix it?


Solution

  • Solved: the code posted works. There was an error in the check method I was calling (CheckTaxNumber). After I fixed the method the validation works as expected.