Asp.net core 如何让远程验证器传入输入值?
我正在使用asp.net核心远程验证程序。我需要传入两个小数,以便验证一个大于另一个 我已重命名控制器操作变量以匹配网页中的元素Id,但这没有影响。我正在将零传递到控制器操作中Asp.net core 如何让远程验证器传入输入值?,asp.net-core,remote-validation,Asp.net Core,Remote Validation,我正在使用asp.net核心远程验证程序。我需要传入两个小数,以便验证一个大于另一个 我已重命名控制器操作变量以匹配网页中的元素Id,但这没有影响。我正在将零传递到控制器操作中 public IActionResult ValidateStandardOfDeviationOfErr(decimal ErrFv, decimal ErrVariance) { if (ErrVariance > ErrFv) return Json("Must be less tha
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>
使用调试器,无论我在输入中键入什么值,只有零传递到验证器。我希望输入的值能够传递给验证操作
这很有效
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
它将数据作为对象传递。因此,您可以创建一个viewModel并接受同名的模型ModelRun
作为操作参数:
ReceivedDataViewModel:
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);
}
我很惊讶[Bind(Prefix=“ModelRun”)]不适用于此。
[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);
}