Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


Workaround to override message “The value {0} is not valid for {1}” in ASP.NET MVC

Share with WhatsApp

This post contains simple information about how you can override or change default error message “The value {0} is not valid for {1}” in MVC.
In my last project I wanted to change default error message “The value {0} is not valid for {1}” for decimal field quantity to “Invalid Quantity”. 

When I google for it I found a solution by which I can achieve it on project level by defining them in a Resource file and then changing “ResourceClassKey” of ClientDataTypeModelValidatorProviderin Global.asax file. I was expecting a one line solution but this was little complex just to achieve simple message change and as our TL was so coward that I knew he will definitely going to reject this solution (happened same :-) ).

Anyways, so I achieved this by overriding this message in controller by manipulating ModelState and that solution got accepted as we wanted this change at two places only. So here I am sharing the same if you too want a quick workaround. This may not be a proper way but to close the bug it has saved my time.

When you define model class like below.

public class ProductViewModel
        [Required(ErrorMessage="Required field.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Required field.")] 
        public decimal Quantity { get; set; }


Error message gets generated in following way when you enter text in decimal type of field.

Default Error message in MVC in model validation

To change the above message I manipulated ModelState like following in controller when form gets posted.

 if (!this.ModelState.IsValid)
                if (this.ModelState["Quantity"].Errors.Count == 1 && this.ModelState["Quantity"].Errors[0].ErrorMessage.Contains("is not valid for Quantity"))
                    this.ModelState["Quantity"].Errors.Add("Invalid Quantity.");
                return View(model);

And that’s it now when you enter text for decimal field following message gets generated.

Changed Error message in MVC in model validation

You can achieve this hack in jQuery too by looping on data attribute. Another workaround is simply make this textbox as numeric textbox by handling it’s key events in jQuery.

Hope this simple post has benefited you. If you know any other way to achieve the same please do share it in comment section below. Thanks.

Download Sample Project

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!