MVC3 Error DataAnnotationsExtensions Using Numeric Attribute

I installed Scott Kirkland's 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 type of check has been seen more than once: number

Any ideas why I get the error message?

+4
source share
2 answers

The quick response just removes the attribute

 [Numeric] 

A longer explanation is that by design, validation already adds the data number-val, because it is a double. By adding Numeric, you duplicate the check.

it works:

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

because the variable is of type string, and you add the Numeric attribute.

Hope this helps

+15
source

I basically had the same problem, and I was able to solve it with the following code snippet: (As said here: ASP.NET MVC - "Validation type names must be unique." )

using System; using System.Web.Mvc; And 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); } 

}

Hope this helps.

0
source

Source: https://habr.com/ru/post/1345053/


All Articles