0

I want to validate user input using the [Remote] attribute. This is how this code looks in the model:

    [Remote(action:"VertifyPeriod",controller:"AllTasks")]
    public string Period { get; set; }

This is what the controller code looks like:

    [AcceptVerbs("GET", "POST")]
    public IActionResult VertifyPeriod(AllTask allTask)
    {            
        if (!string.IsNullOrEmpty(allTask.Period))
        {
                return Json(true);
        }
        return Json($"Wrong period value!");
        
    }

View code:

        <form asp-antiforgery="true" asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Period" class="control-label"></label>
            <input type="text" asp-for="Period" class="form-control" />
            <span asp-validation-for="Period" class="text-danger"></span>
        </div>

If the user leaves the string empty the condition returns true, although it should return false It is listed as null in the database. I noticed peculiarity: If set a breakpoint on a conditional statement, it will never be reached. If I make another check condition, not related to checking for null then everything works fine. IsNullOrWhiteSpace doesn't work either. What could be the problem?

  • 1
    have you actually tried debugging the code? It's a pity that there is not any documentation about how the remote controller action should be implemented (what should it return). Usually it should return a bool value `true` or `false`. Really I don't understand why they think it's OK to not document anything about that. So I doubt that this `Json($"Wrong period value!")` actually means the same as `Json(true)`. You can just ***debug it*** to confirm it. – King King Jan 31 '21 at 13:38
  • Are you sure it's ``VertifyPeriod`` with a 't'? – dumetrulo Jan 31 '21 at 13:44
  • What were both the *value* and the `Length` of `allTask.Period` when you set a breakpoint on it and inspected it from the debugger? You *did* take a look at it, right? My guess is that it's some sort of unprintable characters.. Because I can assure you that `string.IsNullOrEmpty` and `string.IsNullOrWhiteSpace` both work absolutely 100% fine. – pinkfloydx33 Jan 31 '21 at 14:10
  • I tried debugging the code, value exactly null.But I noticed another peculiarity:If set a breakpoint on a conditional statement, it will never be reached. But if I change the conditional operator (for example, check for equality to a certain value), then the breakpoint is reached! – countneuroman Jan 31 '21 at 15:06
  • Have you tried to modify the action to accept a string-type parameter `public IActionResult VertifyPeriod(string period)`? For more information about the `[Remote]` attribute, please check: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0#remote-attribute – Fei Han Feb 01 '21 at 08:28
  • I've tried accept a string type parameter. Also does not work. – countneuroman Feb 01 '21 at 12:16
  • `I noticed peculiarity: If set a breakpoint on a conditional statement, it will never be reached.` You can check this SO thread: https://stackoverflow.com/questions/14707460/remote-attribute-doesnt-trigger-when-field-is-blank – Fei Han Feb 02 '21 at 08:26
  • @FeiHan This is what need, thanks! – countneuroman Feb 03 '21 at 18:39

0 Answers0