1

我将引导程序与 ASP.NET Core 一起使用,并指示表单字段验证错误,当给定字段出现错误时,我想将has-errors类添加到form-groupdiv。视图如下所示:

<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"/>

我知道我可以编写自己的标签助手,但是我很好奇是否有内置的解决方案。

4

1 回答 1

2

我发现你可以使用:

@using Microsoft.AspNetCore.Mvc.ModelBinding

@if(ViewData.ModelState.GetFieldValidationState("Fragment.Content") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
{
    //  something here
}
于 2016-08-20T13:28:24.363 回答