简体   繁体   中英

MVC3 Decimal field not validating properly

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM