我的模型:
public class FPTAssetSummary : IValidatableObject
{
[Required]
[Display(Name = "New Version")]
public int ForatFrom { get; set; }
[Required]
[Display(Name = "Old Version")]
public int ForatTo { get; set; }
public List<FPTFORATExcel> FPTForatVersionList { get; set; }
public IEnumerable<ValidationResult> Validate(
ValidationContext validationContext)
{
if (ForatFrom <= ForatTo)
{
yield return new ValidationResult(
"Old version must be higher than the new version");
}
}
}
我的控制器:
[HttpPost]
public ActionResult ForatExcelCompare(FPTAssetSummary foratcompare)
{
var ExcelIDFrom = foratcompare.ForatFrom;
var ExcelIDTo = foratcompare.ForatTo;
return RedirectToAction("Index", new
{
ForatFrom = ExcelIDFrom,
ForatTo = ExcelIDTo
});
}
目前,我正在将两个整数从一个视图(2 个下拉框)发布到下面的控制器,并使用两个参数(ForatFrom
和ForatTo
)将这两个值传递到索引中。但是我的IValidationObject
方法没有返回ValidationResult
消息。我想我需要检查ForatExcelCompare
方法中的模型状态,但是如果模型状态为假,我需要能够返回到前一个控制器并显示错误消息。有任何想法吗?