我在ASP.NET Core中使用bootstrap并指示表单字段验证错误我想在给定字段有错误时将has-errors
类添加到form-group
div。视图看起来像这样:
<div class="form-group">
<label asp-for="Fragment.Content" class="col-lg-2 control-label "></label>
<div class="col-lg-10">
<textarea asp-for="Fragment.Content" class="form-control content-editor"></textarea>
<span class="help-block">A longer block of help text that breaks onto a new line and may extend beyond one line.</span>
<span asp-validation-for="Fragment.Content"></span>
</div>
</div>
我想做点什么:
<div class="form-group" asp-add-class-if-error="has-errors" for-field="Fragment.Content"/>
我知道我可以编写自己的标记助手,但是如果有内置的解决方案,我很好奇。
答案 0 :(得分:1)
我发现你可以使用:
@using Microsoft.AspNetCore.Mvc.ModelBinding
@if(ViewData.ModelState.GetFieldValidationState("Fragment.Content") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
{
// something here
}