使用数字属性的 MVC3 DataAnnotationsExtensions 错误

发布于 2024-10-26 05:35:08 字数 385 浏览 4 评论 0原文

我已经安装了 Scott 的 Kirkland DataAnnotationsExtensions。

在我的模型中,我有:

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

在我的视图中:

@Html.EditorFor(model => model.expectedcost)

现在,当页面尝试渲染时,我收到以下错误:

以不显眼的方式验证类型名称 客户端验证规则必须是 独特的。以下验证类型 被多次看到:数量

有什么想法为什么我会收到错误吗?

I've installed Scott's Kirkland DataAnnotationsExtensions.

In my model I have:

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

And in my View:

@Html.EditorFor(model => model.expectedcost)

Now, when the page tries to render I get the following error:

Validation type names in unobtrusive
client validation rules must be
unique. The following validation type
was seen more than once: number

Any ideas why I'm getting the error ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

灼痛 2024-11-02 05:35:08

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

[Numeric]

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

这是有效的:

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

因为变量的类型为字符串,并且您要添加数字属性。

希望这有帮助

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

第几種人 2024-11-02 05:35:08

我基本上遇到了同样的问题,我设法用以下代码解决了它:(如此处回答:ASP.NET MVC - “验证类型名称必须是唯一的。”

使用系统;
使用 System.Web.Mvc;
和 ValidationRule:

公共类RequiredIfValidationRule:ModelClientValidationRule
{
私有常量字符串字符=“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 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.

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