简体   繁体   English

MVC3十进制字段未正确验证

[英]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 我的ViewModel中有2个十进制字段

[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 编辑:HTML输入如下所示

<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. Edit2:控制器只检查Modelstate.IsValid,然后将其添加到数据库。

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. 除了Required注释之外,模型中的任何内容都不会导致任何验证。 Anyways, I would suggest looking into DataAnnotationsExtensions for your decimal validations: 无论如何,我建议您查看DataAnnotationsExtensions以获取十进制验证:

http://dataannotationsextensions.org/Numeric/Create http://dataannotationsextensions.org/Numeric/Create

Then you could just decorate your decimal values with [Numeric] 然后,你可以只装饰你decimal与值[Numeric]

You could also try [DataType(DataType.Currency)] if that is acceptable for your Model. 如果模型可以接受,您也可以尝试[DataType(DataType.Currency)]

Do you have a validate method on your Model? 您的模型上有验证方法吗?

EDIT 编辑

Try using float or double instead of decimal . 尝试使用floatdouble而不是decimal I think that the term decimal may be deceiving. 我认为decimal这个词可能是骗人的。 Check out the decimal class here: http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.80).aspx . 在这里查看decimal类: 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. 发生这种情况是因为它正在寻找的参数称为速率,我在模型中也有一个名为Rate的属性。 I just renamed rate to myrate in the controller and all is well now. 我刚刚在控制器中将速率重命名为myrate,现在一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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