Asp.net mvc 用于验证消息的Html.editor

Asp.net mvc 用于验证消息的Html.editor,asp.net-mvc,Asp.net Mvc,使用Html.EditorFor并传入my ViewModel时,可能不会让每个表单元素显示其错误消息 我正在使用验证摘要,因此错误消息会显示两次。一次用于表单元素,然后再次用于摘要。您可以通过使用“*”从ValidationMessage中删除错误消息来删除单个错误,如下例所示。但是,如果您传递了错误消息,则将显示该消息 <%= Html.ValidationMessage("PropertyName", "*") %> 希望有帮助 Eddy您可以通过使用“*”从Validati

使用Html.EditorFor并传入my ViewModel时,可能不会让每个表单元素显示其错误消息


我正在使用验证摘要,因此错误消息会显示两次。一次用于表单元素,然后再次用于摘要。

您可以通过使用“*”从ValidationMessage中删除错误消息来删除单个错误,如下例所示。但是,如果您传递了错误消息,则将显示该消息

<%= Html.ValidationMessage("PropertyName", "*") %>
希望有帮助


Eddy

您可以通过使用“*”从ValidationMessage中删除错误消息来删除单个错误,如下例所示。但是,如果您传递了错误消息,则将显示该消息

<%= Html.ValidationMessage("PropertyName", "*") %>
希望有帮助


Eddy

您可以创建自定义验证摘要,并使用特殊键添加所有错误(在本例中,我使用_表单。例如:

        private const string VALIDATIONSUMMARY_HMTL = "<div class=\"input-validation-error\">{0}</div>";

public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly)
    {
        return ValidationSummary(helper, customErrorOnly, "_FORM");   
    }

    public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly, string errorName)
    {
        if (helper.ViewData.ModelState.IsValid)
        {
            return null;
        }

        string list = "<ul>";
        bool displayList = false;
        foreach (KeyValuePair<string, ModelState> pair in helper.ViewData.ModelState)
        {
            foreach (ModelError error in pair.Value.Errors)
            {
                if (pair.Key.ToUpper() == "_FORM" || !customErrorOnly)
                {
                    list += "<li>" + error.ErrorMessage + "</li>";
                    displayList = true;
                }
            }
        }
        list += "</ul>";

        if (!displayList)
        {
            return null;
        }

        return string.Format(VALIDATIONSUMMARY_HMTL, list);
    }

您可以创建自定义验证摘要,并使用特殊键添加所有错误(在本例中,我使用_表单。例如:

        private const string VALIDATIONSUMMARY_HMTL = "<div class=\"input-validation-error\">{0}</div>";

public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly)
    {
        return ValidationSummary(helper, customErrorOnly, "_FORM");   
    }

    public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly, string errorName)
    {
        if (helper.ViewData.ModelState.IsValid)
        {
            return null;
        }

        string list = "<ul>";
        bool displayList = false;
        foreach (KeyValuePair<string, ModelState> pair in helper.ViewData.ModelState)
        {
            foreach (ModelError error in pair.Value.Errors)
            {
                if (pair.Key.ToUpper() == "_FORM" || !customErrorOnly)
                {
                    list += "<li>" + error.ErrorMessage + "</li>";
                    displayList = true;
                }
            }
        }
        list += "</ul>";

        if (!displayList)
        {
            return null;
        }

        return string.Format(VALIDATIONSUMMARY_HMTL, list);
    }