首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用数字属性的MVC3 DataAnnotationsExtensions错误

使用数字属性的MVC3 DataAnnotationsExtensions错误
EN

Stack Overflow用户
提问于 2011-03-24 08:28:48
回答 2查看 3.2K关注 0票数 4

我安装了斯科特的柯克兰DataAnnotationsExtensions。

在我的模型里:

代码语言:javascript
复制
[Numeric]
public double expectedcost { get; set; }

在我看来:

代码语言:javascript
复制
@Html.EditorFor(model => model.expectedcost)

现在,当页面试图呈现时,我得到以下错误:

非突出客户端验证规则中的

验证类型名称必须是唯一的。多次看到下列验证类型: number

知道我为什么会犯错吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-24 11:03:44

快速的答案是简单地删除属性

代码语言:javascript
复制
[Numeric]

更长的解释是,通过设计,验证已经添加了一个数据-val-数字,因为它的类型为double。通过添加一个数字,您就是在重复验证。

这样做是可行的:

代码语言:javascript
复制
[Numeric]
public string expectedcost { get; set; }

因为变量是string类型的,所以您要添加Numeric属性。

希望这能有所帮助

票数 15
EN

Stack Overflow用户

发布于 2015-10-06 19:21:33

我基本上也遇到了同样的问题,我设法用下面的代码来解决这个问题:(如这里所回答的:ASP.NET MVC - "Validation type names must be unique.")

使用系统;使用System.Web.Mvc;以及ValidationRule:

公共类RequiredIfValidationRule : ModelClientValidationRule {私有Chars = "abcdefghijklmnopqrstuvwxyz";

代码语言:javascript
复制
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);
}

}

我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5416509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档