Asp.net mvc MVC自定义验证属性

Asp.net mvc MVC自定义验证属性,asp.net-mvc,validation,attributes,Asp.net Mvc,Validation,Attributes,当服务器触发ValidationResult时,是否可以获取HTML自定义属性(客户端) 我是这样做的 模型类: public class Model_Test { [Age(18,50)] [Display(Name = "My Age")] public int Age { get; set; } } HTML: @model CustomValidation.Models.Model_Test @using (@Html.BeginForm("Index"

当服务器触发ValidationResult时,是否可以获取HTML自定义属性(客户端)

我是这样做的

模型类:

    public class Model_Test
{
    [Age(18,50)]
    [Display(Name = "My Age")]
    public int Age { get; set; }
}
HTML:

@model CustomValidation.Models.Model_Test
@using (@Html.BeginForm("Index","Test"))
{
    @Html.TextBoxFor(m => m.Age, new { @myValidate="Yes" })
    @Html.TextBoxFor(m => m.Age, new { @myValidate="No" })
    <input type="submit" />
}
@model CustomValidation.Models.model\u测试
@使用(@Html.BeginForm(“Index”,“Test”))
{
@TextBoxFor(m=>m.Age,新的{@myValidate=“Yes”})
@TextBoxFor(m=>m.Age,新的{@myValidate=“No”})
}
自定义属性类:

    public class AgeAttribute : ValidationAttribute
    {
        private readonly int _MinAge = 0;
        private readonly int _MaxAge = 0;
        private const string errorMsg = "{0} must at least {1} or not more than {2}";

        public AgeAttribute(int MinAge, int MaxAge)
            : base(() => errorMsg)
        {
            _MinAge = MinAge;
            _MaxAge = MaxAge;
        }

        //Server-Side Validation
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {



        **// Can we get the HTML Attribute from client side and implement some condition at here???
        // etc...
        // if (html.attribute("myValidate") == "Yes") {
        //    *condition......*
        // } else {
        //    *condition......***
        // }



            if (value != null)
            {
                int data = (int)value;
                if (!(data > (int)_MinAge && data < (int)_MaxAge))
                {
                    return new ValidationResult(null);
                }
            }
            return ValidationResult.Success;
        }
    }
public类AgeAttribute:ValidationAttribute
{
私有只读int_MinAge=0;
私有只读int_MaxAge=0;
private const string errorMsg=“{0}必须至少{1}或不超过{2}”;
公共年龄属性(int MinAge、int MaxAge)
:base(()=>errorMsg)
{
_米纳奇=米纳奇;
_最大年龄=最大年龄;
}
//服务器端验证
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
**//我们可以从客户端获取HTML属性并在这里实现一些条件吗???
//等等。。。
//如果(html.attribute(“myValidate”)=“Yes”){
//*情况*
//}其他{
//*情况***
// }
if(值!=null)
{
int数据=(int)值;
如果(!(数据>(整数)\最小值和数据<(整数)\最大值))
{
返回新的ValidationResult(空);
}
}
返回ValidationResult.Success;
}
}

在我的代码中,我得到了2个
文本框,每个文本框都具有自定义属性
“myValidate=”Yes/No““

我可以将此属性带到服务器端
ValidationResult
进行验证吗?如果没有,还有其他合适的方法吗?

您的方法是正确的,但是一个字段有验证,另一个字段没有验证,最好的方法是使用两个单独的属性,并且只使用自定义属性注释一个字段:

public class Model_Test
{
    [Age(18,50)]
    [Display(Name = "My Age")]
    public int Age { get; set; }

    [Display(Name = "Another age")]
    public int AnotherAge { get; set; }
}

然后,在控制器中,您可以对属性执行您喜欢的操作,并避免使验证代码变得更复杂。

我不明白为什么您希望有两个具有相同属性的文本框和一个没有验证的文本框?实际上,我的目的是创建一个动态表IEnumerable,让用户添加行并在年龄属性上填充一些值,每行可能有不同的条件。您好@Stokedout,谢谢您的回复。但我不想把这些属性一分为二。实际上,我的目的是创建一个动态表
IEnumerable
,让用户添加行并在年龄属性上填充一些值,每行可能有不同的条件。这就是为什么我在考虑如何让服务器端决定应该使用哪种条件。