验证属性 MVC 2 - 检查两个值之一

发布于 2024-09-10 14:41:15 字数 581 浏览 3 评论 0原文

有人可以帮我解决这个问题吗?我试图弄清楚如何检查表单上的两个值,必须填写两个项目之一。如何进行检查以确保已输入其中一项或两项?

我在 ASP.NET MVC 2 中使用视图模型。

下面是一小段代码:

视图:

Email: <%=Html.TextBoxFor(x => x.Email)%>
Telephone: <%=Html.TextBoxFor(x => x.TelephoneNumber)%>

视图模型:

    [Email(ErrorMessage = "Please Enter a Valid Email Address")]
    public string Email { get; set; }

    [DisplayName("Telephone Number")]
    public string TelephoneNumber { get; set; }

我希望提供其中一个详细信息。

感谢您的指点。

Could someone help me with this issue. I'm trying to figure out how to check two values on a form, one of the two items has to be filled in. How do I do a check to ensure one or both of the items have been entered?

I'm using viewmodels in ASP.NET MVC 2.

Here's a little snip of code:

The view:

Email: <%=Html.TextBoxFor(x => x.Email)%>
Telephone: <%=Html.TextBoxFor(x => x.TelephoneNumber)%>

The viewmodel:

    [Email(ErrorMessage = "Please Enter a Valid Email Address")]
    public string Email { get; set; }

    [DisplayName("Telephone Number")]
    public string TelephoneNumber { get; set; }

I want either of these details to be provided.

Thanks for any pointers.

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

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

发布评论

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

评论(1

妞丶爷亲个 2024-09-17 14:41:15

您可以采用与作为文件 -> 新建 -> ASP.NET MVC 2 Web 应用程序一部分的 PropertiesMustMatch 属性大致相同的方式来执行此操作。

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EitherOrAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "Either '{0}' or '{1}' must have a value.";
    private readonly object _typeId = new object();

    public EitherOrAttribute(string primaryProperty, string secondaryProperty)
        : base(_defaultErrorMessage)
    {
        PrimaryProperty = primaryProperty;
        SecondaryProperty = secondaryProperty;
    }

    public string PrimaryProperty { get; private set; }
    public string SecondaryProperty { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            PrimaryProperty, SecondaryProperty);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value);
        object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value);
        return primaryValue != null || secondaryValue != null;
    }
}

该函数的关键部分是 IsValid 函数,它确定两个参数之一是否有值。

与普通的基于属性的属性不同,它应用于类级别并且可以像这样使用:

[EitherOr("Email", "TelephoneNumber")]
public class ExampleViewModel
{
    [Email(ErrorMessage = "Please Enter a Valid Email Address")]
    public string Email { get; set; }

    [DisplayName("Telephone Number")]
    public string TelephoneNumber { get; set; }
}

您应该能够在每个表单中添加所需数量的属性,但是如果您想强制它们在其中输入一个值超过两个框(例如电子邮件、电话或传真),那么您可能最好将输入更改为更多值数组并以这种方式解析它。

You can probably do this in much the same way as the PropertiesMustMatch attribute that comes as part of the File->New->ASP.NET MVC 2 Web Application.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EitherOrAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "Either '{0}' or '{1}' must have a value.";
    private readonly object _typeId = new object();

    public EitherOrAttribute(string primaryProperty, string secondaryProperty)
        : base(_defaultErrorMessage)
    {
        PrimaryProperty = primaryProperty;
        SecondaryProperty = secondaryProperty;
    }

    public string PrimaryProperty { get; private set; }
    public string SecondaryProperty { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            PrimaryProperty, SecondaryProperty);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value);
        object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value);
        return primaryValue != null || secondaryValue != null;
    }
}

The key part of this function is the IsValid function that determines if one of the two parameters has a value.

Unlike normal Property-based attributes, this is applied to the class level and can be used like so:

[EitherOr("Email", "TelephoneNumber")]
public class ExampleViewModel
{
    [Email(ErrorMessage = "Please Enter a Valid Email Address")]
    public string Email { get; set; }

    [DisplayName("Telephone Number")]
    public string TelephoneNumber { get; set; }
}

You should be able to add as many as these as you need per form, but if you want to force them to enter a value into one of more than two boxes (Email, Telephone or Fax for example), then you would probably be best changing the input to be more an array of values and parse it that way.

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