为什么我的是int?值被验证为是否需要?
我有一个整数?查看在客户端验证的模型属性,就像需要的一样。也就是说,如果我将该字段留空,它将不会提交。对于字符串属性,不会发生同样的情况。
为我的编辑器呈现的 HTML 是:
<input type="text" value="" name="StatusIdSearch" id="StatusIdSearch" data-val-number="The field Status must be a number." data-val="true" class="text-box single-line">
我相信 data-val-number
正在导致错误,因为没有什么不是数字,但我无法确定原因。
有什么想法吗?
编辑
视图模型:
public class CompromissoSearchModel
{
// other properties removed for the sake of clarity
[Display(Name = "Status")]
[EnumDataType(typeof(StatusCompromisso))]
public int? StatusIdSearch { get; set; }
// other properties removed for the sake of clarity
}
I have an int? view model property that is validated at client-side as if it was required. That is, if I leave the field blank, it will not submit. The same does not happen for string properties.
The HTML rendered for my editor is:
<input type="text" value="" name="StatusIdSearch" id="StatusIdSearch" data-val-number="The field Status must be a number." data-val="true" class="text-box single-line">
I believe that data-val-number
is causing an error because nothing is not a number, but I cannot determine why.
Any ideas?
Edit
The view-model:
public class CompromissoSearchModel
{
// other properties removed for the sake of clarity
[Display(Name = "Status")]
[EnumDataType(typeof(StatusCompromisso))]
public int? StatusIdSearch { get; set; }
// other properties removed for the sake of clarity
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您看到的消息与必填字段验证无关。您收到此消息是因为
ClientDataTypeModelValidatorProvider
添加客户端数字验证,并忽略类型是否可为空。您可以检查代码你自己:以及
IsNumericType
实现:由于不考虑可为空,因此你总是会得到该验证。在解决方案方面,您需要从使用的提供程序中删除
ClientDataTypeModelValidatorProvider
,或者可能将其替换为不忽略可空值的自定义提供程序。The message you are seeing it's not related to a required field validation. You're gettings this because
ClientDataTypeModelValidatorProvider
adds client numeric validation and it ignores if the type is nullable or nor not. You can check the code yourself:And the
IsNumericType
implementation:Since the nullable is not considered you always get that validation. In terms of solution, you need to remove
ClientDataTypeModelValidatorProvider
from the used providers or maybe replace it with a custom one that does not ignore nullable.您应该能够将以下代码添加到 Global.asax 文件中的
Application_Start
方法中来解决此问题:DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
You should be able to add the following code to your
Application_Start
method in Global.asax file to fix this issue:DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
我遇到了完全相同的问题并设法找到了解决方案。这些解决方案都不适合我,所以我想我会将我的解决方案发布给其他遇到此问题的人。
问题不在于模型绑定程序将字段验证为无效,而是在使用 TryUpdateModel 时,视图模型的可为空属性在数据库实体中不可为空。
更清晰的解释:
视图模型中的“DecimalProperty”可以为空,但在 dbUser 中则不能为空。
I was having the exact same problem and managed to find a solution. None of these solutions worked for me so I thought I'd post my solution for anyone else having this problem.
The problem was not that the model binder was validating the field as invalid, but that when using TryUpdateModel the nullable property of the viewmodel wasn't nullable in the database entity.
Clearer explanation:
"DecimalProperty" in the viewmodel was nullable, but it wasn't nullable in dbUser.