MVC3 Decimal field not validating properly

10,218

Solution 1

Ok, so my problem turned out to be that the Model being posted back to the controller was null. This happened because the argument it is looking for is called rate and I also have a property called Rate in my model. I just renamed rate to myrate in the controller and all is well now.

Solution 2

I'm very surprised that you are getting validation errors for numbers (unless you have a separate validate method that you are not showing). It doesn't look like anything in your model will cause any validation except for the Required annotation. Anyways, I would suggest looking into DataAnnotationsExtensions for your decimal validations:

http://dataannotationsextensions.org/Numeric/Create

Then you could just decorate your decimal values with [Numeric]

You could also try [DataType(DataType.Currency)] if that is acceptable for your Model.

Do you have a validate method on your Model?

EDIT

Try using float or double instead of decimal. I think that the term decimal may be deceiving. Check out the decimal class here: http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.80).aspx .

It may address some of the issues. A decimal looks like this:

decimal myMoney = 300.5m;
Share:
10,218
Jonathan
Author by

Jonathan

Updated on June 06, 2022

Comments

  • Jonathan
    Jonathan almost 2 years

    I've seen a few other questions dealing with localization but that doesn't seem to be the problem here.

    I have 2 decimal fields in my ViewModel

    [DisplayName("Standard Hourly Rate")]
    public decimal Rate { get; set; }
    
    [Required]
    [DisplayName("Daily Travel Rate")]
    public decimal TravelRate { get; set; }
    

    In my view, I have this:

    <div class="editor-label">
        @Html.LabelFor(model => model.Rate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Rate)
        @Html.ValidationMessageFor(model => model.Rate)
    </div>
    
    <div class="editor-label">
        @Html.LabelFor(model => model.TravelRate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.TravelRate)
        @Html.ValidationMessageFor(model => model.TravelRate)
    </div>
    

    This looks like it should be fine but whenever I submit my form, I get a validation error. If I submit any number with decimal or not, I am told the value is invalid. If I enter something that is not a number, I am told it has to be a number.

    Edit: The HTML inputs look like this

    <input type="text" value="" name="Rate" id="Rate" data-val-required="The Standard Hourly Rate field is required." data-val-number="The field Standard Hourly Rate must be a number." data-val="true" class="text-box single-line">
    
    <input type="text" value="" name="TravelRate" id="TravelRate" data-val-required="The Daily Travel Rate field is required." data-val-number="The field Daily Travel Rate must be a number." data-val="true" class="text-box single-line">
    

    Edit2: The controller just checks for Modelstate.IsValid and then adds it to the database.

  • Jonathan
    Jonathan over 12 years
    No, I don't have any validate methods in my model.
  • Rondel
    Rondel over 12 years
    That looks pretty good. That isn't the issue. Try switching up the DataAnnotation and see if anything changes. I didn't think that MVC would autogenerate a number validation
  • Jonathan
    Jonathan over 12 years
    It wasn't found anywhere. Though I believe it is auto-generated since I specified the display name for that field in the model so it must generate that when it tries to validate.
  • Rondel
    Rondel over 12 years
    Yeah that makes sense. I noticed that as soon as I said it. One more thing. In your View, look at the top for a line like this @model ProjectName.RatesViewModel. I bet your View is expecting the PayRate model and is getting the validation from that class.
  • Jonathan
    Jonathan over 12 years
    Actually, if that were the case, it would throw an exception. It's expecting the RatesViewModel; I'm tempted to just turn the decimals into strings and just add [Numeric] but that seems like running away from the problem. I'd also have to convert the strings before inserting them into the database.
  • Rondel
    Rondel over 12 years
    Yeah, it's definitely a strange issue. Try using float or double instead of decimal and see if you get the same crazy behavior. The decimal type may be deceiving.
  • Jonathan
    Jonathan over 12 years
    Well, I'm letting this be for the rest of the week. I'll try that on Tuesday and let you know how it turns out.
  • Jonathan
    Jonathan over 12 years
    Tried this just now with double and I still get the same error. This is really weird.