ASP.NET MVC数据注解属性设置范围从另一个属性值 [英] ASP.NET MVC data annotations attribute Range set from another property value
问题描述
您好我在我的Asp.net MVC模式具有以下
Hi I have following in my Asp.net MVc Model
TestModel.cs
TestModel.cs
public class TestModel
{
public double OpeningAmount { get; set; }
[Required(ErrorMessage="Required")]
[Display(Name = "amount")]
[Range(0 , double.MaxValue, ErrorMessage = "The value must be greater than 0")]
public string amount { get; set; }
}
现在从我的控制器OpeningAmount是分配。
Now from my controller "OpeningAmount " is assign .
Finaly当我提交表单我要检查量一定比OpeningAmonut更大。所以要设置动态范围像
Finaly when I submit form I want to check that "amount" must be greater than "OpeningAmonut" . so want to set Range dynamically like
[Range(minimum = OpeningAmount , double.MaxValue, ErrorMessage = "The value must be greater than 0")]
我不希望只使用jQuery的或JavaScript,因为它仅检查客户端,以便尽可能我可以设置范围属性最小动态比这将是巨大的。
I do not want to use only Jquery or javascript because it will check only client side so possible I can set Range attribute minimum dynamically than it would be great for.
推荐答案
有没有内置的属性,它可以与性能之间的依赖性工作。
There's no built-in attribute which can work with dependence between properties.
所以,如果你想与属性的工作,你必须编写自定义的。
So if you want to work with attributes, you'll have to write a custom one.
硒<一个href=\"http://nickstips.word$p$pss.com/2011/11/05/asp-net-mvc-lessthan-and-greaterthan-validation-attributes/\"相对=nofollow>这里你所需要的例子。
您也可以看看 dataannotationsextensions.org
另一个解决方案是一个验证库的工作,比如(非常漂亮) FluentValidation
Another solution would be to work with a validation library, like (the very nice) FluentValidation .
这篇关于ASP.NET MVC数据注解属性设置范围从另一个属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!