1

I have class Employee

public class Employee
{
  public string LastName { get; set; }
  public decimal Salary { get; set; }
}

In EmployeesController, method

[HttpPostAttribute]
public ActionResult Create(Employee employee)
{

}

If a user enters “1,2,3” in the Salary field of the Employee form, the ModelState becomes invalid.

  1. “1,2,3” is a valid C# decimal number, but more important
  2. why/how/on what basis does Asp.Net MVC check the validity of the input string?

I found no official documentation on this. Can anybody shed light on this?

user669226
  • 661
  • 1
  • 7
  • 16

1 Answers1

0

enter link description hereIt's how the DecimalModelBinder works (see second response, basically the Decimal Model Binder is returning false for 1, 2 and/or 3...). It's see's 1, 2 and 3 as integers only.

Try passing in 1.00 and it'll take it.

It's all about the magic this is the ModelBinders and here. Here's a nice overview.

Here's a sample of how to extend it.

Another overview with some tips for taking advantage of the model binding.

You can also download the source as a way to get to know what it's doing. The codeplex site has some nice information that might help you as well.

EDIT - To answer your questions in your comments...

You can change the error message by using Data Annotations. Check out this also. You can create your own Data Annotations as well if you don't like how the default / built-in work. You can also localize your validation messages.

Here's another nice overview that goes through some of the techniques you can employ for validation. You can add validation directly to the model as well as using Annotations. Of particular interest for you may be validating using the IDataErrorInfo Interface, the IValidatableObject Interface and/or Remote Validation.

Community
  • 1
  • 1
Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
  • 1. Where are error messages? Are they hard-coded in MVC model binders? What if I need non-English error messages? 2. In the case of decimals, what if the user enters a decimal in non-English form? 3. How do I suppress "built-in" error-checking and error messages? – user669226 May 07 '11 at 16:52
  • this question is full of old and removed links and there is no actual explaination or summary of what it was supposed to be in those links. – Maurizio In denmark Feb 18 '15 at 09:47