如何显示与@ Html.ValidationMessageFor多个验证错误? [英] How to display multiple validation errors with @Html.ValidationMessageFor?

查看:463
本文介绍了如何显示与@ Html.ValidationMessageFor多个验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到我的 ModelState.Values​​ [X] .Errors 正确填入一个单一属性的两个验证错误。

I can see that my ModelState.Values[x].Errors is correctly populated with the two validation errors for a single property.

如果我使用 @ Html.ValidationSummary()在我看来,它正确地显示这两个错误....尽管在页面的顶部,而不是下一个对违规的输入。

If I use a @Html.ValidationSummary() in my view, it correctly displays both errors.... albeit at the top of the page, and not next to the offending input.

使用 @ Html.ValidationMessageFor(型号=> model.MyProperty)显示该属性的第一个错误只

我如何同时显示错误,旁边的适当的输入?

How do I show both errors, next to the appropriate input?

推荐答案

一个解决方案是实施的HtmlHelper,做的东西默认ValidationMessageFor行为不同的自己的扩展方法。样品@ Html.ValidationMessagesFor以下方法将连接期间已加入的ModelState多个错误消息的服务器侧的验证(只)。

One solution is to implement your own extension method on HtmlHelper that does something different to the default ValidationMessageFor behavior. The sample @Html.ValidationMessagesFor method below will concatenate multiple error messages that have been added to the ModelState during server-side validation (only).

public static MvcHtmlString ValidationMessagesFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
    var propertyName = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).PropertyName;
    var modelState = htmlHelper.ViewData.ModelState;

    // If we have multiple (server-side) validation errors, collect and present them.
    if (modelState.ContainsKey(propertyName) && modelState[propertyName].Errors.Count > 1)
    {
        var msgs = new StringBuilder();
        foreach (ModelError error in modelState[propertyName].Errors)
        {
            msgs.AppendLine(error.ErrorMessage);
        }

        // Return standard ValidationMessageFor, overriding the message with our concatenated list of messages.
        return htmlHelper.ValidationMessageFor(expression, msgs.ToString(), htmlAttributes as IDictionary<string, object> ?? htmlAttributes);
    }

    // Revert to default behaviour.
    return htmlHelper.ValidationMessageFor(expression, null, htmlAttributes as IDictionary<string, object> ?? htmlAttributes);
}

如果您有您跨集合属于你的模型(例如,交叉检查总数),或检查模型整体应用自定义业务验证这是非常有用的。

This is useful if you have custom business validation you're applying across a collection belonging to your model (for example, cross-checking totals), or checking the model as a whole.

用这个,一个例子,其中Or​​der.LineItems的集合被验证服务器端:

An example using this, where a collection of Order.LineItems are validated server-side:

@using MyHtmlHelperExtensions    // namespace containing ValidationMessagesFor
@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.LineItems)
    <ul>
        @Html.EditorFor(m => m.LineItems)
    </ul>
    @Html.ValidationMessagesFor(m => m.LineItems)
}

这篇关于如何显示与@ Html.ValidationMessageFor多个验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆