如何让我的远程验证器传入输入值?

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

我正在使用 asp.net 核心远程验证器。我需要传递两位小数,以便我可以验证一个大于另一个。

我已重命名控制器操作变量以匹配网页中的元素 ID,但这没有任何影响。我将零传递给控制器​​操作。

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>

在我的视图模型中,我使用了以下注释:

[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; }

使用调试器,无论我在输入中键入什么值,只有零会传递到验证器。我期待将输入的值传递给验证操作。

这个有效

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);
    }

我试过你的代码,它会发送这样的请求:

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

将数据作为 object.So 传递,您可以创建一个 viewModel 并接受具有相同名称 ModelRun 的模型作为操作参数:

收到的数据视图模型:

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

操作:

[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);
}