asp.net-coreremote-validation

How do I get my remote validator to pass in the input values?


I am using the asp.net core remote validator. I need to pass in two decimals so I can validate that one is greater than the other.

I've renamed the controller action variables to match the element Id's in the web page but this had no impact. I am getting zero's passed into the controller action.

public IActionResult ValidateStandardOfDeviationOfErr(decimal ErrFv, decimal ErrVariance)
{
    if (ErrVariance > ErrFv)
        return Json("Must be less than or equal to Std of ERR, Current");
    return Json(true);
}


 <div class="form-row">
                    <div class="form-group col-md-6">
                        <label asp-for="ModelRun.ErrVariance">Std of ERR, Current</label>
                        <input class="form-control form-control-sm" asp-for="ModelRun.ErrVariance" value="0.01" step="0.01" min="0" type="number" />
                        <div class="invalid-feedback" style="display:block;">
                            <span asp-validation-for="ModelRun.ErrVariance"></span>
                        </div>
                        <small class="form-text text-muted">Standard Deviation of ERR, current use[Linear scale].</small>
                    </div>
                    <div class="form-group col-md-6">
                        <label asp-for="ModelRun.ErrFv">Std of ERR, Former</label>
                        <input class="form-control form-control-sm rounded-0" asp-for="ModelRun.ErrFv" value="0.01" step="0.01" min="0" type="number" data-greaterthan-field="stdErrCurrent" data-greaterthan-msg='Must be less than or equal to Std of ERR, Current' />
                        <div class="invalid-feedback" style="display:block;">
                            <span asp-validation-for="ModelRun.ErrFv"></span>
                        </div>
                        <small class="form-text text-muted">Standard Deviation of ERR, former use[Linear scale].</small>
                    </div>
                </div>

In my view model, I am using the following annotations:

[Range(0.0, Double.PositiveInfinity, ErrorMessage = "Must be greater than 0.")]
[Required(ErrorMessage = "Required.")]
public decimal ErrVariance { get; set; }

[Range(0.0, Double.PositiveInfinity, ErrorMessage = "Must be greater than 0.")]
[Required(ErrorMessage = "Required.")]
[Remote("ValidateStandardOfDeviationOfErr", "Model2", AdditionalFields = nameof(ErrVariance))]
public decimal ErrFv { get; set; }

Using the debugger, only zero's are passing into the validator regardless of what values I type into the inputs. I was expecting the typed in values to be passed to the validation action.

This works

public IActionResult ValidateStandardOfDeviationOfErr([FromQuery] Library model)
    {
        if (model.ModelRun.ErrVariance > model.ModelRun.ErrFv)
            return Json("Must be less than or equal to Std of ERR, Current");
        return Json(true);
    }

Solution

  • I try your code and it will send the request like:

    https://localhost:44366/Model2/ValidateStandardOfDeviationOfErr?ModelRun.ErrFv=0.05&ModelRun.ErrVariance=0.09
    

    which passes data as an object.So you could create a viewModel and accept the model with same name ModelRun as the parameter on action:

    ReceivedDataViewModel:

    public class ReceivedDataViewModel
    {
        public decimal ErrVariance { get; set; }
        public decimal ErrFv { get; set; }
    }
    

    Action:

    [AcceptVerbs("Get", "Post")]
    public IActionResult ValidateStandardOfDeviationOfErr(ReceivedDataViewModel ModelRun)
    {
        if (ModelRun.ErrVariance > ModelRun.ErrFv)
            return Json("Must be less than or equal to Std of ERR, Current");
        return Json(true);
    }