ASP.NET Razor Pages检查属性验证状态?

huangapple go评论64阅读模式
英文:

ASP.NET Razor Pages check property validation state?

问题

我正在尝试使用ASP.NET 7.0中的Razor页面编写网站,并尝试使用Bootstrap 5.1表单在客户端上显示服务器端验证消息。

Bootstrap提供了invalid-feedbackvalid-feedback类,这些类会在输入字段经过验证后(在不成功的POST请求之后)应用于输入字段。然而,要有条件地显示这些消息,输入字段必须分别标记为is-invalidis-valid。我如何获取这些信息?

我知道我可以使用HTML助手从HTML助手或IHtmlHelper中获取验证消息:

<label asp-for="Name" class="form-label"></label>
<input asp-for="Name" class="form-select @(IsValid ? "is-valid" : "is-invalid")" />
<div class="invalid-feedback">
    <!-- <span>@Html.ValidationMessageFor(i => i.Name)</span> -->
    <span asp-validation-for="Name"></span>
</div>

我想创建类似第二行的东西,其中我将类应用于输入。然而,当所有这些逻辑位于另一个EditorTemplate内部时,我无法从模型中获取ValidationState。到目前为止,我尝试使用与ValidationMessageFor用于检查该字典的方法相同的方法来获取属性在ModelStateDictionary中使用的键,但它们在该类中不是公开可用的,我还尝试在HtmlHelper中查找可以让我访问该验证状态的方法。

到目前为止,我尝试在Html助手上使用扩展方法,但没有不知道确切键的情况下没有ValidationState方法,这在嵌套属性中是困难的:

Html.ViewContext.ModelState.GetValidationState(key)
英文:

I am trying to write a website using Razor pages in ASP.NET 7.0 and I am trying to get server side validation messages to display on the client using bootstrap 5.1 forms.

Bootstrap provides the class invalid-feedback and valid-feedback which are applied to the input field once the field is validated (after an unsuccessful post request). However to display the messages conditionally the input fields must be tagged with is-invalid or is-valid respectively. How do I get this information?

I know that I can get the validation message from the html helper using either the ValidationMessageTagHelper or the IHtmlHelper:

&lt;label asp-for=&quot;Name&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;input asp-for=&quot;Name&quot; class=&quot;form-select @(IsValid ? &quot;is-valid&quot; : &quot;is-invalid&quot;)&quot; /&gt;
&lt;div class=&quot;invalid-feedback&quot;&gt;
    &lt;!-- &lt;span&gt;@Html.ValidationMessageFor(i =&gt; i.Name)&lt;/span&gt; --&gt;
    &lt;span asp-validation-for=&quot;Name&quot;&gt;&lt;/span&gt;
&lt;/div&gt;

I want to make something similar to the second line where I apply the class to the input. However I cannot get the ValidationState from the Model, especially when all this logic is inside another EditorTemplate. So far I tried to get the Key that the property uses in the ModelStateDictionary using the same methods that ValidationMessageFor use to check that dictionary, but they are not publicly available in that class and I also tried to find methods in the HtmlHelper that give me access to that validation state.

So far I tried to use an extension method on the Html helper, but there is no ValidationState method without knowing the exact key, which is difficult in nested properties:

Html.ViewContext.ModelState.GetValidationState(key)

答案1

得分: 2

根据我的评论,我发现NameFor函数可以实现我要的功能:

public static class HtmlExtensions {
    /// <inheritdoc cref="ModelStateDictionary.GetValidationState(string)"/>
    /// <param name="property">提供键的属性。</param>
    public static ModelValidationState GetValidationState<TModel, TPropertyType>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TPropertyType>> property)
        => helper.ViewContext.ModelState.GetValidationState(helper.NameFor(property));

    public static string GetValidityClass<TModel, TPropertyType>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TPropertyType>> property)
        => helper.GetValidationState(property) switch {
            ModelValidationState.Invalid => "is-invalid",
            ModelValidationState.Valid => "is-valid",
            _ => string.Empty,
        };
}

现在我可以使用这个扩展来应用类似这样的类:

<div class="col-md-6">
    <label asp-for="Name" class="form-label"></label>
    <input asp-for="Name" class="form-control @Html.GetValidityClass(i => i.Name)" />
    <div class="invalid-feedback"><span asp-validation-for="Name"></span></div>
</div>
英文:

As in my comment I figured the NameFor function does what I am looking for:

public static class HtmlExtensions {
    /// &lt;inheritdoc cref=&quot;ModelStateDictionary.GetValidationState(string)&quot;/&gt;
    /// &lt;param name=&quot;property&quot;&gt;Property that provides the key.&lt;/param&gt;
    public static ModelValidationState GetValidationState&lt;TModel, TPropertyType&gt;(this IHtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TPropertyType&gt;&gt; property)
        =&gt; helper.ViewContext.ModelState.GetValidationState(helper.NameFor(property));

    public static string GetValidityClass&lt;TModel, TPropertyType&gt;(this IHtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TPropertyType&gt;&gt; property)
        =&gt; helper.GetValidationState(property) switch {
            ModelValidationState.Invalid =&gt; &quot;is-invalid&quot;,
            ModelValidationState.Valid =&gt; &quot;is-valid&quot;,
            _ =&gt; string.Empty,
        };
}

Now I can use this extension to apply the class like this:

&lt;div class=&quot;col-md-6&quot;&gt;
    &lt;label asp-for=&quot;Name&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
    &lt;input asp-for=&quot;Name&quot; class=&quot;form-control @Html.GetValidityClass(i =&gt; i.Name)&quot; /&gt;
    &lt;div class=&quot;invalid-feedback&quot;&gt;&lt;span asp-validation-for=&quot;Name&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月15日 19:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定