asp.net-mvc – 数据注释 – 如何用MVC3中的Web.Config值替换Ran
发布时间:2020-12-16 03:38:02 所属栏目:asp.Net 来源:网络整理
导读:如何用MVC3中的Web.Config值替换Range值? [Range(5,20,ErrorMessage = "Initial Deposit should be between $5.00 and $20.00")public decimal InitialDeposit { get; set; } web.config中: add key="MinBalance" value="5.00"/add key="MaxDeposit" value
|
如何用MVC3中的Web.Config值替换Range值?
[Range(5,20,ErrorMessage = "Initial Deposit should be between $5.00 and $20.00")
public decimal InitialDeposit { get; set; }
web.config中: <add key="MinBalance" value="5.00"/> <add key="MaxDeposit" value="20.00"/> 解决方法
您需要创建一个继承自RangeAttribute并实现IClientValidatable的自定义属性.
public class ConfigRangeAttribute : RangeAttribute,IClientValidatable
{
public ConfigRangeAttribute(int Int) :
base
(Convert.ToInt32(WebConfigurationManager.AppSettings["IntMin"]),Convert.ToInt32(WebConfigurationManager.AppSettings["IntMax"])) { }
public ConfigRangeAttribute(double Double) :
base
(Convert.ToDouble(WebConfigurationManager.AppSettings["DoubleMin"]),Convert.ToDouble(WebConfigurationManager.AppSettings["DoubleMax"]))
{
_double = true;
}
private bool _double = false;
public override string FormatErrorMessage(string name)
{
return String.Format(ErrorMessageString,name,this.Minimum,this.Maximum);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(this.ErrorMessage),ValidationType = "range",};
rule.ValidationParameters.Add("min",this.Minimum);
rule.ValidationParameters.Add("max",this.Maximum);
yield return rule;
}
protected override ValidationResult IsValid(object value,ValidationContext validationContext)
{
if (value == null)
return null;
if (String.IsNullOrEmpty(value.ToString()))
return null;
if (_double)
{
var val = Convert.ToDouble(value);
if (val >= Convert.ToDouble(this.Minimum) && val <= Convert.ToDouble(this.Maximum))
return null;
}
else
{
var val = Convert.ToInt32(value);
if (val >= Convert.ToInt32(this.Minimum) && val <= Convert.ToInt32(this.Maximum))
return null;
}
return new ValidationResult(
FormatErrorMessage(this.ErrorMessage)
);
}
}
用法示例: [ConfigRange(1)]
public int MyInt { get; set; }
[ConfigRange(1.1,ErrorMessage = "This one has gotta be between {1} and {2}!")]
public double MyDouble { get; set; }
第一个示例将返回默认错误消息,第二个示例将返回您的自定义错误消息.两者都将使用web.config中定义的范围值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 一个始终可用于编写的事件日志源?
- [译]ASP.NET Core 2.0 网址重定向
- asp.net-web-api – 如何从Web API响应中删除标头?
- asp.net-mvc – VS2012 ProjectTypeGuids在安装ASP.NET和We
- asp.net-mvc – 搜索页面MVC路由(隐藏动作,没有斜线,如SO)
- asp.net-mvc – 在MVC 5应用程序中使用autofac in为SignalR
- asp.net-core-1.0 – ASP.NET Core 1.0中的package.json
- ASP.net RequiredFieldValidator VisualStudio 2012
- asp.net – 增加会话TimeOut
- asp.net-mvc – MVC 4 SimpleMembership – 登录后为什么We
推荐文章
站长推荐
- asp.net-mvc-3 – 具有确认对话框的MVC3 Actionl
- 如何在ASP.NET vNext MVC 6(beta1)项目之间共享视
- 从ASP.Net MVC视图访问应用程序设置
- asp.net-mvc – 自动发布使用TFS和MSBuild构建,无
- asp.net-mvc – FluentValidation Autofac Valid
- asp.net – 有一个ClientScriptManager.Register
- ASP.NET DateTime Picker
- asp.net-mvc – 如何在控制器中显示警报消息
- asp.net – 在数据库中存储SHA512密码哈希
- asp.net-mvc – 添加分页MVC和Azure表存储
热点阅读
