为什么我的是int?值被验证为是否需要?

发布于 2025-01-01 07:07:56 字数 752 浏览 3 评论 0原文

我有一个整数?查看在客户端验证的模型属性,就像需要的一样。也就是说,如果我将该字段留空,它将不会提交。对于字符串属性,不会发生同样的情况。

为我的编辑器呈现的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2025-01-08 07:07:56

您看到的消息与必填字段验证无关。您收到此消息是因为 ClientDataTypeModelValidatorProvider 添加客户端数字验证,并忽略类型是否可为空。您可以检查代码你自己

private static IEnumerable<ModelValidator> GetValidatorsImpl(
    ModelMetadata metadata, 
    ControllerContext context) 
{
    Type type = metadata.RealModelType;
    if (IsNumericType(type)) {
        yield return new NumericModelValidator(metadata, context);
    }
}

以及 IsNumericType 实现:

private static bool IsNumericType(Type type) 
{
    // strip off the Nullable<>
    Type underlyingType = Nullable.GetUnderlyingType(type); 
    return _numericTypes.Contains(underlyingType ?? type);
}

由于不考虑可为空,因此你总是会得到该验证。在解决方案方面,您需要从使用的提供程序中删除 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:

private static IEnumerable<ModelValidator> GetValidatorsImpl(
    ModelMetadata metadata, 
    ControllerContext context) 
{
    Type type = metadata.RealModelType;
    if (IsNumericType(type)) {
        yield return new NumericModelValidator(metadata, context);
    }
}

And the IsNumericType implementation:

private static bool IsNumericType(Type type) 
{
    // strip off the Nullable<>
    Type underlyingType = Nullable.GetUnderlyingType(type); 
    return _numericTypes.Contains(underlyingType ?? type);
}

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.

情深如许 2025-01-08 07:07:56

您应该能够将以下代码添加到 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;

酒与心事 2025-01-08 07:07:56

我遇到了完全相同的问题并设法找到了解决方案。这些解决方案都不适合我,所以我想我会将我的解决方案发布给其他遇到此问题的人。

问题不在于模型绑定程序将字段验证为无效,而是在使用 TryUpdateModel 时,视图模型的可为空属性在数据库实体中不可为空。

更清晰的解释:

TryUpdateModel(dbUser, "", new[]{
    "DecimalProperty"
}));

视图模型中的“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:

TryUpdateModel(dbUser, "", new[]{
    "DecimalProperty"
}));

"DecimalProperty" in the viewmodel was nullable, but it wasn't nullable in dbUser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文