问题
I assume the validation check will allow any input value from 1 to 30. But, it prompts for error when I input a value with 4 to 9 in this field. Any advise? Please...
remarks: My project is based on "MVC4 Web Application + Razor View Engine + C# .net".
using System.ComponentModel.DataAnnotations;
[Range(1, 30, ErrorMessageResourceType = typeof(Resources.ErrorMessage), ErrorMessageResourceName = "RangeErrorMsg")]
public int HowManyDays { get; set; }
Below is my Razor view source
<div class="editor-label">
@Resources.Booking.Duration
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HowManyDays)
@Html.ValidationMessageFor(model => model.HowManyDays)
</div>
I have no way to upload my screen dump as I need at least 10 reputation to do so. Weird, it shows alert for value in range of 4 to 9 ONLY. Btw, I have "DataAnnotationsExtensions.MVC3" NuGet package installed. But, I did try to unstalled it without luck.
回答1:
Your second argument in EditorFor
doesn't make any sense.
Take a look at EditorFor help or just on examples of its use on SO (this site).
This probably breaks your view and you get no validation.
To begin with, just remove the comma and the number, see if that works, if not look for examples on how to use EditorFor
.
Edit
Your comment renders my answer completely irrelevant. You should be accurate as to what you put for public to see. Anyway, try to see if this successfully answered question solves your problem: Range DataAnnotation Doesn't Seem to Be Working
Please let me know if this helps. Thanks.
来源:https://stackoverflow.com/questions/15299718/range-data-annotation-is-not-working-as-expected-any-idea