且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC - "验证类型名称必须是唯一的"

更新时间:2022-12-09 20:19:31

我基本上有同样的问题,我设法用下面这段code来解决它:

I basically had the same problem and I managed to solve it with the following piece of code:

using System;
using System.Web.Mvc;

和有效性规则的:

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.