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 7926465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:46:52+00:00 2026-06-03T18:46:52+00:00

I am using MVC 3 + unobstrusive validation. For some field I am using

  • 0

I am using MVC 3 + unobstrusive validation.

For some field I am using remote validation too; inside remote validation, I do some checks whose can return errors or just warnings (I would like to take advantage of ajax validation just to give warnings too, not just blocking errors).
I distinct the warnings by the validation errors with the “Info” prefix inside de description text.

So, does exist a way to cycle all validation errors, maintaining just warnings displayed and set errors off according the displayed text?

I was thinking of using an ActionFilterAttribute, or to force the ModelState.Valid = true after cycling and checking all the validation errors…

Here’s an extract of my remote validation routine, with a WarningCheck attribute:

   [WarningCheck]
   public JsonResult CheckMyField(string myfield) 
    {

        //....some check...if ok I do `return Json(true, JsonRequestBehavior.AllowGet);`
        //...if just a warning, I do the follow...

        string warn = String.Format(CultureInfo.InvariantCulture,
            "Info: some info....");
        ModelState.AddModelError(TicketHD, esiste);
        return Json(warn, JsonRequestBehavior.AllowGet);

    }

    [AttributeUsage(AttributeTargets.All)]
    public class WarningCheckAttribute : ActionFilterAttribute
    {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
       {  
             //.... here I'd like to cycle my warnings and if possible maintaining just the text display and set errors off...is it possible?
       }
     }

EDIT (how to disable client side validation for specific warnings)

Following @CDSmith (@Alfalfastrange) suggestions for server side, I succeed to disable the client side validation too, when a specific text is contained inside each validation error. In particular, my needs were to disable both client & server side validation error just when the errors contained “Info:” text.
Here’s the code I am using for client side behaviour:

       .... 
                    $("#ticketfrm").submit();
                    var isvalid = true;
                    var errmark = $("#ticketfrm .field-validation-error span"); 
                    $(errmark).each(function () {
                        var tst = $(this).text();
                        if (!(tst.indexOf("Info") != -1))
                            isvalid = false; //if the val error is not a warn, the it must be a real error!
                        });

                    if (isvalid) {
                        var form = $('#ticketfrm').get(0); //because I'm inside a jquery dialog
                         $.removeData(form, 'validator'); 
                        jQuery('#ticketfrm').unbind('submit').submit();
                    }

                    $("#ticketfrm").submit();   
                }
            .....

I hope this may help many people…I worked many hours to get this working! I do not think it must be the more elegant solution, but IT WORKS! 🙂
While for server side validation, please read the marked solution.

If you find this useful, please mark it. Thank you!

  • 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-03T18:46:54+00:00Added an answer on June 3, 2026 at 6:46 pm

    I’m not sure I understand the question as well as you’re explaining but what I hear you saying is you need a way of looping thru the ModelState errors and then maintaining the text values of the errors but not show them as errors?? Is that what you are saying?

    Well for starters, ModelState is nothing more than a DictionaryList which you can iterate through with no problem.

    Something like this can do that:

    public ActionResult SomeAction(SomeModel model) {
        if(ModelState.IsValid) {
            // do cool stuff with model data
        }
        var errorMessages = GetModelStateErrors(ModelState);
        foreach (var errorMessage in errors) {
            // do whatever you want with the error message string here
        }
    }
    

    ModelError contains an ErrorMessage property and Exception property

    internal static List<string> GetModelStateErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
        var errors = new List<ModelError>();
        errors = modelStateDictionary.SelectMany(item => item.Value.Errors).ToList();
    } 
    

    Not sure if this helps or not but if it points you in the right direction then cool 🙂

    Update

    Ok here is what I came up with and it works for me with my test app.

    First let me lay out what I have so you can copy and replicate

    Here’s my model

    public class EmployeeViewModel {
    
        public int ID { get; set; }
    
        [Display(Name = "First Name")]
        [Required(ErrorMessage = "Error")]
        public string FirstName { get; set; }
    
        [Display(Name = "Last Name")]
        [Required(ErrorMessage = "Error")]
        public string LastName { get; set; }
    
        [Display(Name = "Username")]
        public string Username { get; set; }
    
        [Display(Name = "Email Address")]
        public string EmailAddress { get; set; }
    }
    

    Here’s a simple view that uses this model

    @model TestApp.Models.EmployeeViewModel
    
    <h2>Test</h2>
    
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>EmployeeViewModel</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.LastName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Username)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Username)
                @Html.ValidationMessageFor(model => model.Username)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    

    Here’s the controller and actions that I used

    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using TestApp.Models;
    
    namespace TestApp.Controllers {
    
        public class HomeController : Controller {
    
            public ActionResult Index() {
                return RedirectToAction("Test");
            }
    
            public ActionResult Test() {
                var model = new EmployeeViewModel();
                return View(model);
            }
    
            [HttpPost]
            public ActionResult Test(EmployeeViewModel model) {
                // Force an error on this property - THIS should be the only real error that gets returned back to the view
                ModelState.AddModelError("", "Error on First Name");
    
                if(model.EmailAddress == null) // Add an INFO message
                    ModelState.AddModelError("", "Email Address Info");
                if (model.Username == null) // Add another INFO message
                    ModelState.AddModelError("", "Username Info");
    
                // Get the Real error off the ModelState
                var errors = GetRealErrors(ModelState);
    
                // clear out anything that the ModelState currently has in it's Errors collection
                foreach (var modelValue in ModelState.Values) {
                    modelValue.Errors.Clear();
                }
                // Add the real errors back on to the ModelState
                foreach (var realError in errors) {
                    ModelState.AddModelError("", realError.ErrorMessage);
                }
                return View(model);
            }
    
            private IEnumerable<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
                var errorMessages = new List<ModelError>() ;
                foreach (var keyValuePair in modelStateDictionary) {
                    if (keyValuePair.Value.Errors.Count > 0) {
                        foreach (var error in keyValuePair.Value.Errors) {
                            if (!error.ErrorMessage.Contains("Info")) {
                                errorMessages.Add(error);
                            }
                        }
                    }
    
                }
                return errorMessages;
            }
        }
    }
    

    You can write GetRealErrors as LINQ instead if you prefer:

    private IEnumerable<ModelError> GetRealErrors(IEnumerable<KeyValuePair<string, ModelState>> modelStateDictionary) {
        var errorMessages = new List<ModelError>() ;
        foreach (var keyValuePair in modelStateDictionary.Where(keyValuePair => keyValuePair.Value.Errors.Count > 0)) {
            errorMessages.AddRange(keyValuePair.Value.Errors.Where(error => !error.ErrorMessage.Contains("Info")));
        }
        return errorMessages;
    }
    

    I hope that gives you what you were looking for, it works the way I think I understand that you wanted. Let me know

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

Sidebar

Related Questions

I'm using client-side, unobtrusive validation in MVC 3. I have a field called MinPrice
im using mvc framework and i have learned some techniques that help me with
I just created a sample MVC3 application to learn validation. It is using DataAnnotations.
I'm using asp.net mvc 3 with unobtrusive jquery validation. I have a form that
I'm using the unobtrusive jQuery client side validation in MVC 3. I have a
I'm using MVC 3 with unobtrusive javascript for client validation. I have a table
I'm using asp.net mvc 3 with jquery unobtrusive validation. I recently changed from standard
I am using asp.net mvc 3 and I keep getting the following error. Validation
Using MVC 3 RTM and MvcContrib/FluentHtml version 3.0.51.0, I can't get the jQuery client
Just getting started using MVC in ASP.NET, I'm going to have it so users

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.