0

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?

TheMixy
  • 1,049
  • 15
  • 36
  • Do you mean `return Json(check.ErrorMessage);` is called while debugging? Is there any demo to reproduce your issue? I made a test, fail to reproduce your issue. – Edward Sep 03 '19 at 05:05
  • yes, return Json(check.ErrorMessage) is called while debugging. I have other validation on the form (like "required" for example and it works/shows). What kind of demo do you have in mind? – TheMixy Sep 03 '19 at 05:32
  • Share us a project which could reproduce your issue. – Edward Sep 03 '19 at 05:41
  • My project has a data layer linked to our SQL server (which is only accessible via our active domain) so I cant share it, because it will not work outside of our environment. Is there some other way we can try and figure this out? Is there something additional I can check for while debbuging? – TheMixy Sep 03 '19 at 06:11
  • 1
    You could create asp.net core built-in template with your view model and controller, try to reproduce your issue with this new template. – Edward Sep 03 '19 at 06:55
  • Tao, when I was preparing a template I noted an error in my checking method (CheckTaxNumber). After I corrected the error validation works as expected. – TheMixy Sep 03 '19 at 07:49
  • How should I mark this thread/post? Should I delete it? – TheMixy Sep 03 '19 at 07:49
  • You could post your answer, and accept it. – Edward Sep 03 '19 at 07:56

1 Answers1

0

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.

TheMixy
  • 1,049
  • 15
  • 36
  • 1
    There was error in this line: `var check = _clients.CheckTaxNumber(taxNumber);` in the CheckTaxNumber method. There was nothing wrong with the remote validation itself or the way I used it in cshtml. Since it's more than a year ago I do not remember the exact error... – TheMixy Nov 12 '20 at 05:21