RangeAttribute 数据注释未按预期工作

发布于 2024-09-30 05:06:30 字数 980 浏览 6 评论 0原文

我正在开发一个 ASP.NET MVC2 应用程序,其中有一个 User 类,其属性名为 SecurityQuestionID ,如下所示:

public class User
{
    [RangeAttribute(1, 5, ErrorMessage = "Security Question is required")]
    [DisplayName("Security Question")]
    public virtual int SecurityQuestionID { get; set; }
}

填充 SecurityQuestionID 字段从视图中的下拉列表中,如下所示:

<%: Html.LabelFor(model => model.SecurityQuestionID)%>
<%: Html.DropDownListFor(model => model.SecurityQuestionID, ViewData["securityQuestions"] as SelectList,"Choose a Question",null) %>
<%: Html.ValidationMessageFor(model => model.SecurityQuestionID)%>

控制器通过使用视图数据将安全问题发送到视图,如下所示:

ViewData["securityQuestions"] = new SelectList(_securityQuestionService.GetAll(), "SecurityQuestionID", "Question");

如果我没有从下拉列表中选择问题并点击提交按钮,则“安全问题”字段为必填项。”消息将显示,而不是“安全问题为必填项”。谁能帮助我理解我在这里做错了什么?

I am developing an ASP.NET MVC2 application in which I have a User class with a property named SecurityQuestionID as follows:

public class User
{
    [RangeAttribute(1, 5, ErrorMessage = "Security Question is required")]
    [DisplayName("Security Question")]
    public virtual int SecurityQuestionID { get; set; }
}

The SecurityQuestionID field gets populated from a dropdown in the view as follows:

<%: Html.LabelFor(model => model.SecurityQuestionID)%>
<%: Html.DropDownListFor(model => model.SecurityQuestionID, ViewData["securityQuestions"] as SelectList,"Choose a Question",null) %>
<%: Html.ValidationMessageFor(model => model.SecurityQuestionID)%>

The controller sends the security questions to the view by using view data as follows:

ViewData["securityQuestions"] = new SelectList(_securityQuestionService.GetAll(), "SecurityQuestionID", "Question");

If I don't select a question from the dropdown and hit the submit button, then "The Security Question field is required." message is displayed instead of "Security Question is required". Can anyone help me understand what I am doing wrong here?

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

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

发布评论

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

评论(2

累赘 2024-10-07 05:06:30

如果整数不可为空,则默认情况下需要它。因此范围验证没有失败,但必需的验证失败。
您可能希望通过添加 required 属性来在需要时指定单独的错误消息。

[Required(ErrorMessage="Security Question is required")]

If the integer is not nullable, then it is required by default. So the range validation isn't failing but Required validation is failing.
You may want to specify a separate error message for when it is required by adding a required attribute.

[Required(ErrorMessage="Security Question is required")]
酒绊 2024-10-07 05:06:30

它可能会抛出该错误,因为它是必填字段。请参阅http://forums.asp.net/p/1391688/2977493.aspx。这与以下事实一致:相关字段被定义为 int,而不是 int?,因此它不能假定为 null价值。

换句话说,它没有击中您的范围验证器。

It's probably throwing that error because it is a required field. See http://forums.asp.net/p/1391688/2977493.aspx. This is consistent with the fact that the field in question is defined as an int, and not an int?, so it cannot assume a null value.

In other words, it's not hitting your range validator.

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