客户端验证需要mvc 2号错误

时间:2012-08-27 22:51:05

标签: asp.net-mvc client-side-validation

我有一个使用此模型的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"
            }
        ]
    }

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

1 个答案:

答案 0 :(得分: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操作方法上的十进制参数,则会更清楚。