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.
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.