tags:

views:

113

answers:

3

I'm using ValidationMessage control in MVC. When validating each property, it may have more than one error message to show, but the ValidationMessage only displays the first error message in the list.

Here is an example:

ModelState["Key"] = new ModelState(); ModelState["Key"].Errors.Add("Error 1"); ModelState["Key"].Errors.Add("Error 2");

and in the html I have: <%= Html.ValidationMessage("Key")%>

which displays: "Error 1"

I want to see all error messages on the page which will be "Error 1 Error 2"

Any idea how to do it?

+1  A: 

With just out-of-the-box MVC, you'll have to add a ValidationSummary:

<%= Html.ValidationSummary() %>

That will show all ModelErrors.

Ray Vernagus
A: 

Also in your Controller Action you can check the

ModelState.IsValid

and if its false, just return the View and the ValidationSumary will be populated.

Jack Marchetti
A: 

I had exactly the same problem, so I created an extension method for HtmlHelper as replacement for the MVC ValidationMessage method.

The benefit of this over ValidationSummary method is that it displays error message per field so you can place it right next to each field (same as ValidationMessage method).

public static string AllValidationMessage(this HtmlHelper helper, string modelName)
{
    StringBuilder builder = new StringBuilder();
    TagBuilder ulTag = new TagBuilder("ul");
    ulTag.AddCssClass("u-error-list");

    builder.Append(ulTag.ToString(TagRenderMode.StartTag));
    if (helper.ViewData.ModelState.ContainsKey(modelName) &&
        helper.ViewData.ModelState[modelName].Errors.Count > 0)
    {
        foreach (var err in helper.ViewData.ModelState[modelName].Errors)
        {
            TagBuilder liTag = new TagBuilder("li") { InnerHtml = HttpUtility.HtmlEncode(err.ErrorMessage) };
            liTag.AddCssClass("u-error-item");
            builder.Append(liTag.ToString());
        }
    }
    builder.Append(ulTag.ToString(TagRenderMode.EndTag));

    var msgSpan = helper.ValidationMessage(modelName, "{placeholder}");

    return msgSpan.ToHtmlString().Replace("{placeholder}", builder.ToString());
}

public static string AllValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
    return HtmlHelperExtensions.AllValidationMessage(helper, ExpressionHelper.GetExpressionText(expression));
}

Edit: added AllValidationMessageFor method

Bill Yang