Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:55:33+00:00 2026-06-08T01:55:33+00:00

I am presented with a situation where I would like to use Html.ValidationSummary() in

  • 0

I am presented with a situation where I would like to use Html.ValidationSummary() in my views to display a generic error message. Maybe something like "There was a problem with the information you entered." but without getting a list of every single thing that went wrong (as per DataAnnotations).

side note: I’ve found references to people using if(ViewContext.ViewData.ModelState.IsValid){ ... } in their views and foregoing ValidationSummary altogether. I am not really impressed with that approach, to say the least.

It’s important that the ModelStateDictionary still contains a key for each model element which had a validation error (so that Html.EditorFor, et al. write the correct css classes.)

What I ended up with and seems to work is:

public static class ModelStateHelpers
{
    public static void Empty(this ModelStateDictionary @this)
    {
        foreach (KeyValuePair<string, ModelState> state in @this)
        {
            if (state.Value.Errors.Any())
            {
                state.Value.Errors.Clear();
                state.Value.Errors.Add(String.Empty);
            }
        }
    }
}

Followed by this in my class:

if (StuffWentWrong) {
    this.ModelState.Empty();
    this.ModelState.AddModelError("", "Something went wrong...");
}

As intended, the Html.ValidationSummary is smart enough to suppress html structure for ModelStates in the dictionary whose error messages are blank; and I also end up with text fields decorated with error validation attributes, such as <input class="input-validation-error text-box single-line" data-val="true" data-val-required="The User Name field is required." id="UserName" name="UserName" type="text" value="" />

Does this particular solution smell badly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:55:35+00:00Added an answer on June 8, 2026 at 1:55 am

    I’ve found references to people using
    if(ViewContext.ViewData.ModelState.IsValid){ ... } in their views and
    foregoing ValidationSummary altogether. I am not really impressed with
    that approach, to say the least.

    Why not? I think this is a reasonable solution. You say you want a special use case for displaying the validation summary, which is a view concern. You shouldn’t have to manipulate anything in the controller or otherwise fight the framework to accomplish this.

    As to whether or not your current solution with a static helper class + method, I think that smells worse than checking ViewContext.ViewData.ModelState.IsValid from razor. I commented that you should download the source code for the ValidationSummary HtmlHelper and look into retooling it to suit your needs, which is probably the best answer to your question. You could essentially encapsulate the aforementioned if statement into a custom HtmlHelper, which is an appropriate place to put code for view concerns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in a situation where I would like to accept a LOT of
I am in the following situation. When the collection.length = 0 I would like
I have situation where a user can manipulate a large set of data (presented
I have the following situation, presented in the picture. Grey div is the parent
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key
I have presented the code below. The compiler throws error when I overload an
There is a code snippet presented in The Pragmatic Programmer on page 143 as:
I would like to support HTTP Basic Authentication in my UIWebView. At the moment,
I am using ASP.NET MVC3 with Razor and would like to define a route
Suppose there are objects making subscriptions to a socket server like so: socket.on('news', obj.socketEvent)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.