Range data annotation is not working as expected, any idea?

给你一囗甜甜゛ 提交于 2019-12-11 09:06:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!