asp.Net   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – MVC不引人注目的范围验证动态值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对我的模型有一个价值,它必须在我的模型上的另外两值的范围内。

例如:

public class RangeValidationSampleModel
{
    int Value { get; set; }

    int MinValue { get; set; }

    int MaxValue { get; set; }
}

当然,我不能将这些Min / MaxValues传递给我的DataAnnotations属性,因为它们必须是常量值。

我确定我需要建立自己的验证属性,但是我没有这么做,不能把我的想法放在一起,应该如何工作。

我搜索了大约一个小时,并且已经看到了各种解决方案来构建自定义验证,但是找不到任何东西来解决这个特殊的问题,使用MVC3不显眼的验证。

解决方法

您可以为此编写自定义验证属性:
public class DynamicRangeValidator : ValidationAttribute,IClientValidatable
{
    private readonly String _minPropertyName;
    private readonly String _maxPropertyName;
    public DynamicRangeValidator(String minPropertyName,String maxPropertyName)
    {
        _minPropertyName = minPropertyName;
        _maxPropertyName = maxPropertyName;
    }

    protected override ValidationResult IsValid(object value,ValidationContext validationContext)
    {
        var minProperty = validationContext.objectType.GetProperty(_minPropertyName);
        var maxProperty = validationContext.objectType.GetProperty(_maxPropertyName);
        if (minProperty == null)
        {
            return new ValidationResult(String.Format("Unknown property {0}",_minPropertyName));
        }
        if (maxProperty == null)
        {
            return new ValidationResult(String.Format("Unknown property {0}",_maxPropertyName));
        }

        int minValue = (int)R_151_11845@inProperty.GetValue(validationContext.objecTinstance,null);
        int maxValue = (int)R_151_11845@axProperty.GetValue(validationContext.objecTinstance,null);
        int currentValue = (int)value;
        if (currentValue <= minValue || currentValue >= maxvalue)
        {
            return new ValidationResult(
                String.Format(
                    Errormessage,minValue,maxValue
                )
            );
        }

        return null;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "dynamicrange",Errormessage = this.Errormessage,};
        rule.ValidationParameters["minvalueproperty"] = _minPropertyName;
        rule.ValidationParameters["maxvalueproperty"] = _maxPropertyName;
        yield return rule;
    }
}

然后用它来装饰你的视图模型:

public class RangeValidationSampleModel
{
    [DynamicRangeValidator("MinValue","MaxValue",Errormessage = "Value must be between {0} and {1}")]
    public int Value { get; set; }
    public int MinValue { get; set; }
    public int MaxValue { get; set; }
}

那么你可以有一个控制器提供视图:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new RangeValidationSampleModel
        {
            Value = 5,MinValue = 6,MaxValue = 8
        });
    }

    [httpPost]
    public ActionResult Index(RangeValidationSampleModel model)
    {
        return View(model);
    }
}

并且当然是:

@model RangeValidationSampleModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script type="text/javascript">
    $.validator.unobtrusive.adapters.add('dynamicrange',['minvalueproperty','maxvalueproperty'],function (options) {
            options.rules['dynamicrange'] = options.params;
            if (options.message != null) {
                $.validator.messages.dynamicrange = options.message;
            }
        }
    );

    $.validator.addMethod('dynamicrange',function (value,element,params) {
        var minValue = parseInt($('input[name="' + params.minvalueproperty + '"]').val(),10);
        var maxValue = parseInt($('input[name="' + params.maxvalueproperty + '"]').val(),10);
        var currentValue = parseInt(value,10);
        if (isNaN(minvalue) || isNaN(maxvalue) || isNaN(currentvalue) || minValue >= currentValue || currentValue >= maxvalue) {
            var message = $(element).attr('data-val-dynamicrange');
            $.validator.messages.dynamicrange = $.validator.format(message,maxvalue);
            return false;
        }
        return true;
    },'');
</script>

@using (Html.beginForm())
{
    <div>
        @Html.LabelFor(x => x.value)
        @Html.EditorFor(x => x.value)
        @Html.ValidationmessageFor(x => x.value)
    </div>
    <div>
        @Html.LabelFor(x => x.Minvalue)
        @Html.EditorFor(x => x.Minvalue)
    </div>
    <div>
        @Html.LabelFor(x => x.Maxvalue)
        @Html.EditorFor(x => x.Maxvalue)
    </div>
    <button type="submit">OK</button>
}

显然,自定义适配器注册应该在一个外部的javascript文件中执行,以避免污染视图,但为了这个目的和简洁的这篇文章我把它放在视图内。

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – MVC不引人注目的范围验证动态值全部内容,希望文章能够帮你解决asp.net-mvc – MVC不引人注目的范围验证动态值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。