Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net MVC - ValidationSummary not displaying

I have a strange problem whereby the ValidationSummary is not being displayed. However, the ValidationMessage's are being displayed. I've checked the output page source and it's not as though they are in a colour that is obscuring them. I'm using the RC. Any ideas?

Edit: Break point set at ValidationSummary shows:

ViewData.ModelState.Values[1].ErrorMessage = ""
ViewData.ModelState.Values[1].Exception.InnerException.Message = "4a is not a valid value for Int32"

Does ValidationSummary use ErrorMessage and ValidationMessage use InnerException.Message?

My view code is:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<App.Models.PurchaseOrdersView>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>Edit</title>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>

    <%= Html.ValidationSummary() %>

    <% Html.BeginForm("Edit", "PurchaseOrder", FormMethod.Post); %>
    <table>
        <tr>
            <td>
                Purchase Order Id:
            </td>
            <td>
                <%= Html.TextBox("PurchaseOrderId", Model.PurchaseOrderId)%>
                <%= Html.ValidationMessage("PurchaseOrderId")%>
            </td>
        </tr>
        <tr>
            <td>
                Date:
            </td>
            <td>
                <%= Html.TextBox("Date", Model.Date.ToString("dd-MMM-yyyy"))%>
                <%= Html.ValidationMessage("Date")%>
            </td>
        </tr>
    </table>
    <input type="submit" value="Save" />

    <% Html.EndForm(); %>

</asp:Content>
like image 209
Alan T Avatar asked Feb 13 '09 13:02

Alan T


2 Answers

Try

<%= Html.ValidationSummary(false) %>
like image 175
Dmitriy Radionov Avatar answered Oct 19 '22 04:10

Dmitriy Radionov


You don't say what the errors are that are not displaying, but there are some errors which will be displayed in ValidationMessage but not in ValidationSummary. I think that this is a bug in the Release Candidate, but I am open to other interpretations. In particular, consider this line from the ValidationSummary source code:

string errorText = GetUserErrorMessageOrDefault(modelError, null /* modelState */);

Note that nothing is passed for modelState. Now contrast that with ValidationMessage:

... GetUserErrorMessageOrDefault(modelError, modelState) ...

Finally, let's look at GetUserErrorMessageOrDefault:

    private static string GetUserErrorMessageOrDefault(ModelError error, ModelState modelState) {
        if (!String.IsNullOrEmpty(error.ErrorMessage)) {
            return error.ErrorMessage;
        }
        if (modelState == null) {
            return null;
        }

        string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
        return String.Format(CultureInfo.CurrentCulture, MvcResources.Common_ValueNotValidForProperty, attemptedValue);
    }

What this tells us is that if you specify a custom error message when you add an error to the model state, it will be displayed. If, however, an exception is added (there is one overload for AddModelError which takes an exception, another which takes a string; implementing IDataErrorInfo works like the string case), instead of a string error message, it will only be displayed if modelState is non-null, and then we'll give you a generic message instead of the error message on the exception.

Update

Does ValidationSummary use ErrorMessage and ValidationMessage use InnerException.Message?

Yes, that's more or less the effect. Like I said, I think this is a bug.

Update2

Microsoft updated the GetUserErrorMessageOrDefault function as seen here.

private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
    {
        if (!String.IsNullOrEmpty(error.ErrorMessage))
        {
            return error.ErrorMessage;
        }
        if (modelState == null)
        {
            return null;
        }

        string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
        return String.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), attemptedValue);
    }
like image 38
Craig Stuntz Avatar answered Oct 19 '22 02:10

Craig Stuntz