5

In ASP.NET MVC, when we call a post action with some data, we check ModelState and in case some validation error, it would be falst. For a big Enter User Information form, it is annoying to expand each Value and look at the count to see which Key (9 in attached example image) has validation error. Wondering if someone knows an easy way to figure out which element is causing validation error.

enter image description here

SBirthare
  • 5,117
  • 4
  • 34
  • 59
  • The top answer from here should help you http://stackoverflow.com/questions/2845852/asp-net-mvc-how-to-convert-modelstate-errors-to-json You can use a LINQ query to find the errors easily. – Jason Evans Jun 27 '14 at 13:53
  • Well you must have validation messages in your form where it will be shown. in case you are just showing errors only for properties which you are providing as Input, then i would recommend to show error summary at the top of page with the help of html helper. This way all errors irrespect of there property will get shown there. OR If you want it to find programmatically, then just write a linq query for this dictionary and store error list in temp variable for the time being. – K D Jun 27 '14 at 13:55
  • In simple ways without changing the code just use this extention on your View for the time being.... http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationsummary(v=vs.118).aspx – K D Jun 27 '14 at 13:57
  • Check this for reference http://20fingers2brains.blogspot.com/2013/03/validationsummary-html-helper-in-mvc3.html – K D Jun 27 '14 at 13:58
  • I was actually looking through a debugger point. I reviewed the link @Jason provided, it provide enough information for me. Going to keep the question for a day to see if anyone has any other idea. Thanks all for comments. – SBirthare Jun 27 '14 at 13:59
  • @KD - I got validation summary on but was doing a quick implementation of multi tab view with multiple forms. While debugging got a thought how can I effectively check which key has problem. So posted the quesiton :) – SBirthare Jun 27 '14 at 14:01

4 Answers4

8

In VS2015+, you can use LINQ in the Immediate Window, which means you can just run the following:

ModelState.SelectMany(
    x => x.Value.Errors, 
    (state, error) => $"{state.Key}:  {error.ErrorMessage}"
)
Arithmomaniac
  • 4,604
  • 3
  • 38
  • 58
4

I propose to write a method:

namespace System.Web
{
    using Mvc;

    public static class ModelStateExtensions
    {
        public static Tuple<string, string> GetFirstError(this ModelStateDictionary modelState)
        {
            if (modelState.IsValid)
            {
                return null;
            }

            foreach (var key in modelState.Keys)
            {
                if (modelState[key].Errors.Count != 0)
                {
                    return new Tuple<string, string>(key, modelState[key].Errors[0].ErrorMessage);
                }
            }

            return null;
        }
    }
}

Then during debugging open Immediate Window and enter:

ModelState.GetFirstError()
cryss
  • 4,130
  • 1
  • 29
  • 34
  • Wow nice, I tried it and it works well. I like this being simple and no overhead. Just an extension method lying around in Web layer to be utilized when needed. Thanks. – SBirthare Jun 30 '14 at 06:52
2

Sounds like you're looking for debugger enhancements. I recently came across this product in the visual studio gallery.

http://visualstudiogallery.msdn.microsoft.com/16acdc63-c4f1-43a7-866a-67ff7022a0ac

I have no affiliation with them, and haven't used it. It's also a trial version and have no idea how much it costs for the full thing.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I tried it, it works well. Gives a window with filter so we can search Count = 1 and locate the corresponding key. While this tool is nice its paid. Unless in next 30 days I find more reasons to buy it, I would use another method to solve this not so frequent problem. Thanks for suggestion. – SBirthare Jun 30 '14 at 06:24
2

If you're more focused on the debugger side of things, have a go with the trial copy of OzCode. It enhances the Visual Studio IDE by replacing the usual debugging tooltip with it's own, more powerful, debugging tooltip. It's hard to epxlain with words, check out their website, they have a gallery of features on there.

I've been playing around with the beta for a few weeks, and it's proved a very valuable tool. You can query against data in the debugger using OzCode. For example, you could query items in the ModelState by filtering against the Values collection.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • Thanks Jason, I will try this tool sometime. At the moment going to use extension method that cryss suggested. Thanks for suggestion though. – SBirthare Jun 30 '14 at 06:53