0

I've seen an answer to this here Greater Than or Equal To Today Date validation annotation in MVC3 and was attempting to use the Remote option suggested in my .Net Core 3.1 application. I have put a breakpoint in my action but it's never hitting it, just throwing the validation over and over again regardless of the date entered (or blank). Here's what I've tried:

As annotation on my data model:

[Remote("ValidateDateEqualOrGreater", "AccountViewer", HttpMethod="POST", ErrorMessage = "Date cannot be prior to today's date")]
public DateTimeOffset? PostDate {get; set; }

Inside my controller:

[HttpPost]
public JsonResult ValidateDateEqualOrGreater(DateTimeOffset Date)
{
    if (Date == null || Date >= DateTimeOffset.UtcNow.Date)
    {
        return Json(true);
    }
    return Json(false);
}

It doesn't appear that the code is getting called at all, as I'm not hitting any of the breakpoints in it. What else do you have to do to get the Remote() annotation working?

nonesuch
  • 167
  • 3
  • 16
  • Did you already take a look at this? - https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#remote-attribute – Sai Gummaluri Aug 05 '20 at 21:06
  • Yes, I seem to be doing the right thing but it's not calling the action. – nonesuch Aug 05 '20 at 21:14
  • A fundamental check, are you sure this call is getting triggered from the client side? Can you confirm that from the network tab of your browser? – Sai Gummaluri Aug 05 '20 at 21:18
  • Good call. I just checked and confirmed that it's getting a 200 response back from ValidateDateEqualOrGreater in my network tab – nonesuch Aug 05 '20 at 21:25
  • Could you also confirm the request and response payloads, please? Is the data we are looking at, null by any chance (maybe the data being sent to server)? What is the response body in the case of 200? – Sai Gummaluri Aug 05 '20 at 21:39

2 Answers2

0

I think you forget the HttpMethod ="POST" in your RemoteAttribute définition. ;)

Orkad
  • 630
  • 5
  • 15
0

I have major egg on my face. My breakpoint wasn't set properly. The function failed because the blank date comes through as "01/01/0001" and not null and I had to account for that. It's working now. Sorry for the firedrill and thanks for all the assistance.

Working function:

[HttpPost]
        public JsonResult ValidateDateEqualOrGreater(DateTimeOffset Dt)
        {
            DateTime nulltime = new DateTime(0001, 1, 1, 0, 0, 0);

            var nulldt = new DateTimeOffset(nulltime, new TimeSpan(0, 0, 0));
            if (Dt == null || Dt == nulldt || Dt >= DateTimeOffset.UtcNow.Date)
            {
                return Json(true);
            }
            return Json(false);
        }
nonesuch
  • 167
  • 3
  • 16