【发布时间】:2022-08-17 16:18:37
【问题描述】:
我有两个控制速度和速度单位。速度是一个数字文本框,速度单位是一个下拉列表。
如果填写速度单位,则需要速度字段,反之亦然。
为了实现这个验证,我使用了remote data annotation。但是在前端,验证不会触发。
我的视图模型:
[AbpDisplayName(OE_TenantConsts.LocalizationSourceName, \"idb_Incident.Speed\")]
[Range(0.0, double.MaxValue, ErrorMessage = \"Please enter a value of {1} or more\")]
[Remote(action: \"VerifySpeedAndUnit\", controller: \"Event\", AdditionalFields = nameof(SpeedUnitId))]
public decimal? Speed { get; set; }
[AbpDisplayName(OE_TenantConsts.LocalizationSourceName, \"idb_Incident.SpeedUnitId\")]
[Remote(action: \"VerifySpeedAndUnit\", controller: \"Event\", AdditionalFields = nameof(Speed))]
public int? SpeedUnitId { get; set; }
我的控制器(事件控制器 -> VerifySpeedAndUnit):
[AcceptVerbs(\"GET\", \"POST\")]
[AllowAnonymous]
public IActionResult VerifySpeedAndUnit(decimal? speed, int? speedUnitId)
{
if ((speed.HasValue && speedUnitId == DropdownConstants.DefaultSelectedSpeedUnitId) || (speed.HasValue && speedUnitId == null))
{
return Json(\"When entering a speed, you need to select the speed scale as well.\");
}
if (speedUnitId != DropdownConstants.DefaultSelectedSpeedUnitId && speed.HasValue && speedUnitId != null)
{
return Json(\"When selecting a speed scale, you need to enter a speed value as well.\");
}
return Json(true);
}
我的速度剑道控制:
<div class=\"mb-5 col-md-6 idb_Incident_Speed\">
@Html.LabelFor(model => model.Speed, htmlAttributes: new { @class = \"control-label col-12\" })
<div class=\"col-12\">
<kendo-numerictextbox deferred=\"true\" for=\"Speed\"
format=\"#.##\"
min=\"0\"
name=\"Speed\"
decimals=\"2\"
placeholder=\"Enter Speed at time of Incident\">
</kendo-numerictextbox>
@Html.ValidationMessageFor(model => model.Speed, \"\", new { @class = \"text-danger\" })
</div>
</div>