1

我有一个具有此模型的 ASP.NET MVC2 项目:

    [DisplayName("Valor(%)")]
    [Required(ErrorMessage = "Este dato es requerido")]
    public decimal HOJ_ATI_CONT_SAPO_VALOR1  { get; set; }

    [DisplayName("Valor(%)")]
    public decimal HOJ_ATI_CONT_SAPO_VALOR2 { get; set; }

看法:

Html.EnableClientValidation();

以及输出客户端验证:

   {
        "FieldName": "HOJ_ATI_CONT_SAPO_VALOR1",
        "ReplaceValidationMessageContents": true,
        "ValidationMessageId": "HOJ_ATI_CONT_SAPO_VALOR1_validationMessage",
        "ValidationRules": [
            {
                "ErrorMessage": "Este dato es requerido",
                "ValidationParameters": {},
                "ValidationType": "required"
            },
            {
                "ErrorMessage": "The field Valor(%) must be a number.",
                "ValidationParameters": {},
                "ValidationType": "number"
            }
        ]
    },
    {
        "FieldName": "HOJ_ATI_CONT_SAPO_VALOR2",
        "ReplaceValidationMessageContents": true,
        "ValidationMessageId": "HOJ_ATI_CONT_SAPO_VALOR2_validationMessage",
        "ValidationRules": [
            {
                "ErrorMessage": "The Valor(%) field is required.",
                "ValidationParameters": {},
                "ValidationType": "required"
            },
            {
                "ErrorMessage": "The field Valor(%) must be a number.",
                "ValidationParameters": {},
                "ValidationType": "number"
            }
        ]
    }

为什么十进制字段添加默认的必需属性?我需要删除所需的验证类型。

4

1 回答 1

0

你的小数必须是可空的,否则 MVC 会使它们成为必需的;

[DisplayName("Valor(%)")]
[Required(ErrorMessage = "Este dato es requerido")]
public decimal? HOJ_ATI_CONT_SAPO_VALOR1  { get; set; }

[DisplayName("Valor(%)")]
public decimal? HOJ_ATI_CONT_SAPO_VALOR2 { get; set; }

我相信这是因为没有空小数这样的东西——如果小数没有设置,那么它将被存储为“0”。如果您尝试将空白字段传递给 MVC 操作方法上的小数参数,这一点会更清楚。

于 2012-08-27T23:45:10.300 回答