【问题标题】:CustomAttribute that validates 2 fields验证 2 个字段的 CustomAttribute
【发布时间】:2023-03-27 12:50:01
【问题描述】:

所以我有这两个输入字段 Fromdate 和 ToDate,

在 MVC 中,我想创建一个 CustomAttribute 以确保 Fromdate 在 ToDate 之前。

这可能吗?如果是这样怎么办?或者有什么替代方法?

谢谢

【问题讨论】:

  • 这对您有帮助吗? codeproject.com/Articles/301022/…
  • 它是否适用于多个领域?
  • 正如文章开头所说:There are lots of validation attributes available in MVC 3 but sometimes we may require specific type of validation which is dependent on some other properties as well。有问题的例子是I come across a situation where I need validation on a set of checkboxes. Validation I need was simple that at least one checkbox from the set must be checked,所以他申请的领域不止一个,是的。他正在检查是否至少选中了 3 个复选框中的一个。

标签: c# asp.net-mvc custom-attributes


【解决方案1】:

MVC FoolProof Validation 是解决此问题的好方法。它通过引用在服务器端运行,并通过脚本包含传播客户端。

http://foolproof.codeplex.com/

这里是一个属性的例子:

public class EventViewModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public DateTime Start { get; set; }

    [Required]
    [GreaterThan("Start")]
    public DateTime End { get; set; }
}

查看该日期的 GreaterThan 属性。这样就不用自己写了。

你也可以将它nuGet到你的项目中:http://nuget.org/packages/foolproof

PM> Install-Package foolproof

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2011-08-03
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多