MVC3 DataAnnotationsExtensions error using numeric attribute

蓝咒 提交于 2019-12-03 17:24:13

The quick answer is simply remove the attribute

[Numeric]

The longer explanation is that by design, validation already adds a data-val-number because it's of type double. By adding a Numeric you are duplicating the validation.

this works:

[Numeric]
public string expectedcost { get; set; }

because the variable is of type string and you are adding the Numeric attribute.

Hope this helps

Jaco B

I basically had the same problem and I managed to solve it with the following piece of code: (As answered here: ASP.NET MVC - "Validation type names must be unique.")

using System; using System.Web.Mvc; And the ValidationRule:

public class RequiredIfValidationRule : ModelClientValidationRule { private const string Chars = "abcdefghijklmnopqrstuvwxyz";

public RequiredIfValidationRule(string errorMessage, string reqVal,
    string otherProperties, string otherValues, int count)
{
    var c = "";
    if (count > 0)
    {
        var p = 0;
        while (count / Math.Pow(Chars.Length, p) > Chars.Length)
            p++;

        while (p > 0)
        {
            var i = (int)(count / Math.Pow(Chars.Length, p));
            c += Chars[Math.Max(i, 1) - 1];
            count = count - (int)(i * Math.Pow(Chars.Length, p));
            p--;
        }
        var ip = Math.Max(Math.Min((count) % Chars.Length, Chars.Length - 1), 0);
        c += Chars[ip];
    }

    ErrorMessage = errorMessage;
    // The following line is where i used the unique part of the name
    //   that was generated above.
    ValidationType = "requiredif"+c;
    ValidationParameters.Add("reqval", reqVal);
    ValidationParameters.Add("others", otherProperties);
    ValidationParameters.Add("values", otherValues);
}

}

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!