在我的视图模型中,我有一个具有以下属性的对象:
[Display(Name = "Fiscal Year")]
[ValidFiscalYear(FiscalYear = "FiscalYear", ErrorMessage = "Please enter a valid fiscal year.")]
public int FiscalYear { get; set; }
该属性具有属性ValidFiscalYear
,该属性具有名为FiscalYear
的字符串参数。我使用此参数来获取另一个名为FiscalYear
的模型属性的值。反过来,此属性用于确定验证。
以下是视图模型中的FiscalYear
属性:
[Required]
[Display(Name = "FiscalYear")]
public int FiscalYear { get; set; }
这是属性:
public sealed class ValidFiscalYearAttribute : ValidationAttribute, IClientValidatable
{
public string FiscalYear { get; set; }
public override bool IsValid(object value)
{
if (value == null)
{
return false;
}
var currentFiscalYearString = HttpContext.Current.Request[FiscalYear];
var currentFiscalYear = int.Parse(currentFiscalYearString);
var fiscalYear = (int)value;
return fiscalYear >= currentFiscalYear && fiscalYear <= currentFiscalYear + 10;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new ModelClientValidationRule[] { new ModelClientValidationRule
{
ValidationType = "fiscalyear",
ErrorMessage = this.ErrorMessage }
};
}
}
这是我在客户端的jQuery:
jQuery.validator.addMethod('fiscalyear', function (value, element, params) {
var currentFiscalYear = $("#FiscalYear").val();
return (value >= currentFiscalYear && value <= +currentFiscalYear + 10);
}, '');
jQuery.validator.unobtrusive.adapters.add('fiscalyear', {}, function (options) {
options.rules['fiscalyear'] = true;
options.messages['fiscalyear'] = options.message;
});
因此,一切正常,但我需要创建一个自定义错误消息。我的问题是如何在客户端和服务器端创建读取错误消息
Fiscal Year must be between **currentFiscalYear** and **currentFiscalYear + 10**.
currentFiscalYear
部分充当变量。
我想在错误消息中使用属性类中的currentFiscalYear
变量。这可能吗
答案 0 :(得分:0)
您可以通过设置ErrorMessage
属性来创建自定义错误消息。
public sealed class ValidFiscalYearAttribute : ...
{
public ValidFiscalYearAttribute()
{
ErrorMessage = "My custom message";
}
...
}
您的自定义验证程序会继承此属性。
答案 1 :(得分:0)
你需要通过&#34;当前财政年度&#34;从服务器到客户端,因为只有服务器知道该值。您可以使用以下语法来获取从服务器传递的额外值。
jQuery.validator.unobtrusive.adapters.add("fiscalyear", ["currentfiscalyear"], function (options) {
options.rules["fiscalyear"] = options.params.currentfiscalyear;
options.messages["fiscalyear"] = options.message;
jQuery.validator.unobtrusive.parse();
});
jQuery.validator.addMethod('fiscalyear', function (value, element, param) {
var currentFiscalYear = parseInt(param);
return (value >= currentFiscalYear && value <= currentFiscalYear + 10);
});
以下是您如何发送&#34;当前会计年度&#34;来自服务器的值以及正确的错误消息:
public sealed class ValidFiscalYearAttribute : ValidationAttribute, IClientValidatable
{
public ValidFiscalYearAttribute() : base("Fiscal Year must be between {0} and {1}.")
{
}
public string FiscalYear { get; set; }
public override string FormatErrorMessage(string name)
{
var currentFiscalYearString = HttpContext.Current.Request[FiscalYear];
var currentFiscalYear = int.Parse(currentFiscalYearString);
return string.Format(ErrorMessageString, currentFiscalYear, currentFiscalYear + 10);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// You should not check for null but should not return an error as return error for null should be the responsibility of the Required attribute
if(value == null) return null;
var currentFiscalYearString = HttpContext.Current.Request[FiscalYear];
var currentFiscalYear = int.Parse(currentFiscalYearString);
var fiscalYear = (int)value;
var valid = fiscalYear >= currentFiscalYear && fiscalYear <= currentFiscalYear + 10;
if (!valid)
{
var message = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(message);
}
return null;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var currentFiscalYearString = HttpContext.Current.Request[FiscalYear];
var currentFiscalYear = int.Parse(currentFiscalYearString);
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "fiscalyear";
rule.ValidationParameters.Add("currentfiscalyear", currentFiscalYear);
yield return rule;
}
}
正如您所看到的那样,有许多活动部件。我没有测试过这段代码,并在浏览器中键入了大部分内容。如果您发现某些内容无法正常工作,请告诉我。