3

I render a Validation Summary in my MVC 5 App as below:

 @Html.Partial("_ValidationSummary", @ViewData.ModelState)

and the Partial View code is as below:

@model ModelStateDictionary

<div class="@(Html.ViewData.ModelState.IsValid ? "validation-summary-valid" : "validation-summary-errors") panel panel-danger"
     data-valmsg-summary="true">
    <div class="panel-heading">
        Please, correct the following errors:
    </div>
    <div class="panel-body">
        <ul>
            @foreach (var modelError in Model.SelectMany(keyValuePair => keyValuePair.Value.Errors))
            {
                <li>@modelError</li>
                <li>@modelError.ErrorMessage</li> 
            }
        </ul>
    </div>
</div>

This is working quite nicely - in that the error messages display for fields that are in error.

However - The name of the Field on the model that is error is not added - so I have a field for mobile number and for home phone and for work phone. If i put abc into each field then in my validation summary I get 3 error messages saying "Phone Number is Invalid" but I would like to add the field to the error message so that I can add that to the error message to the User will know which of the fields is in error?

Is there an easy way to tweak my current code to achieve this?

3 Answers 3

1

You can use the ModelMetaData, found in the ViewData, to retrieve the display name of the key :

@foreach (var key in ViewData.ModelState.Keys)
{
    var modelState = ViewData.ModelState[key];
    var property = ViewData.ModelMetadata.Properties.FirstOrDefault(p => p.PropertyName == key);

    if (property != null)
    {
        var displayName = property.DisplayName;

        foreach (var error in modelState.Errors)
        {
            <li>@displayName: @error.ErrorMessage</li>
        }
    }
    else
    {
        foreach (var error in modelState.Errors)
        {
            <li>@error.ErrorMessage</li>
        }
    }
}

Make sure you add

@using System.Linq

at the top of the view.

You can also use a <label> to allow the user to click on the display name to automatically focus the input field:

<li><label for="@key">@displayName</label>: @error.ErrorMessage</li>
Sign up to request clarification or add additional context in comments.

Comments

0

You can try below LINQ query

foreach (var modEr in ModelState.Select(st => new { Field = st.Key, Errors = st.Value.Errors.Select(er => er.ErrorMessage) }))
    {
        //modEr.Field //Field name
        //modEr.Errors //List of errors
    }

Additionally there is a MVC defined HTML helper to show validation summary, refer http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationsummary(v=vs.118).aspx

1 Comment

I would advise following the link posted in this answer rather than the linq statement. Also, see asp.net/mvc/tutorials/older-versions/models-(data)/… for a good example showing both models and the HTML.
0

You can just use data annotations against each of the fields in your model.

2 Comments

a little more detail would be nice
The field will be validated on the Razor form via ValidationSummary. Decorate the field that requires validation in the Razor view via like this: [DisplayName("Title")] public string InvariantTitle { get; set; } If the field does not pass validation, the ValidationSummary message will use "Title" (not "Invariant Title") in the message shown on the UI. The form message will look like this: "The Title field is required."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.