如果字段有效,如何检查剃须刀视图?

时间:2016-08-18 22:02:10

标签: c# razor asp.net-core asp.net-core-tag-helpers

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

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

1 个答案:

答案 0 :(得分:1)

我发现你可以使用:

@using Microsoft.AspNetCore.Mvc.ModelBinding

@if(ViewData.ModelState.GetFieldValidationState("Fragment.Content") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
{
    //  something here
}