数据注释自定义验证器在 MVC2 中不起作用(正如我所期望的)

发布于 2024-11-07 22:57:15 字数 1838 浏览 1 评论 0原文

在开始之前...我无法轻松地将项目迁移到 MVC3。所以。

我遇到的问题是我定义了一个自定义验证器属性来检查字符串属性 StringLengthInRangeAttribute 的最大和最小长度。

当控制器调用 ModelState.IsValid 时,在 Passengers 列表上,只有 Date 属性的验证会抛出无效,而没有提供任何内容。我想这意味着我的问题不在于自定义验证器,而在于所有验证?

更新(为了清楚起见,附加信息):

我有这个问题的两个症状:
1.字符串上的必需验证器在时不会触发

2.我的自定义验证器永远不会被调用(我设置的断点永远不会被命中)。

模型:

 public class Passenger
 {
    [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "RequireNumber")]
    public int Number { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "RequireSurname")]
    [StringLengthInRange(MinLength = 2, MaxLength = 30, ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "MaxLengthSurname")]
    public string Surname { get; set; }
 } 

自定义验证器:

public class StringLengthInRangeAttribute:ValidationAttribute
{
    public int MinLength { get; set; }
    public int MaxLength { get; set; }

    public override bool IsValid(object value)
    {
        if (((string)value).Length < MinLength)
        {
            return false;
        }

        if (((string)value).Length > MaxLength)
        {
            return false;
        }

        return true;
    }
}

控制器操作:

 public ViewResult TailorHoliday(List<SearchAndBook.Models.ViewModels.Passenger> passengers, 
        int leadPassengerIndex)
    {
        if(!ModelState.IsValid)
        {
            return View("PassengerDetails", GetBookingState(_currentSession));
        }
    //...
    return View();
   }

任何建议表示赞赏。这是我第一次使用数据注释,所以我已经准备好为错过一些东西而感到愚蠢!

Before I start ... I can't easily migrate the project to MVC3. So.

The problem I'm having is that I've defined a custom validator attribute to check the max AND min length of a string property, StringLengthInRangeAttribute.

When the Controller calls ModelState.IsValid, on a list of Passengers only the validation of a Date property is throwing invalid, when nothing has been supplied. I guess that means my problem is not with the custom validator but all validation?

Update (additional info for clarity):

I have two symptoms of this problem :
1.The Required validator on the strings doesn't fire when they are empty
and
2.My custom validator never gets called (a breakpoint I set never gets hit).

Model:

 public class Passenger
 {
    [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "RequireNumber")]
    public int Number { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "RequireSurname")]
    [StringLengthInRange(MinLength = 2, MaxLength = 30, ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
        ErrorMessageResourceName = "MaxLengthSurname")]
    public string Surname { get; set; }
 } 

Custom Validator:

public class StringLengthInRangeAttribute:ValidationAttribute
{
    public int MinLength { get; set; }
    public int MaxLength { get; set; }

    public override bool IsValid(object value)
    {
        if (((string)value).Length < MinLength)
        {
            return false;
        }

        if (((string)value).Length > MaxLength)
        {
            return false;
        }

        return true;
    }
}

Controller Action:

 public ViewResult TailorHoliday(List<SearchAndBook.Models.ViewModels.Passenger> passengers, 
        int leadPassengerIndex)
    {
        if(!ModelState.IsValid)
        {
            return View("PassengerDetails", GetBookingState(_currentSession));
        }
    //...
    return View();
   }

Any advice appreciated. This is the first time I've used Data Annotations, so I'm quite prepared to feel stupid for missing something!

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

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

发布评论

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

评论(1

酒儿 2024-11-14 22:57:15

如果您检查 ModelState 属性的内容(在调试器中),您应该能够看到在 ModelState.Keys 中验证的每个属性,并且对于每个值,您都可以在 中看到实际状态模型状态.值。如果我看一下你的管制员,你似乎会发布完整的乘客名单。您还应该检查您的值是否真的已发布(使用 Firebug 或 Fiddler)。也许您的字段位于表单之外。

也许您还应该显示部分视图来发现错误。

If you check the content of the ModelState property (in the debugger) you should be able to see every property that gets validated in ModelState.Keys and for each value you see the actual state in the ModelState.Values. If I look at your controller you seem to post a whole list of passengers. You should check as well, whether your values are really posted (use Firebug or Fiddler). Maybe your fields are outside the form.

Maybe you should show part of your view as well to spot the bug.

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