My Model:
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");
}
}
}
My Controller:
[HttpPost]
public ActionResult ForatExcelCompare(FPTAssetSummary foratcompare)
{
var ExcelIDFrom = foratcompare.ForatFrom;
var ExcelIDTo = foratcompare.ForatTo;
return RedirectToAction("Index", new
{
ForatFrom = ExcelIDFrom,
ForatTo = ExcelIDTo
});
}
Currently I am posting two integers from a view (2 dropdownboxes), to the controller below and passing the two values into the index with the two parameters (ForatFrom
and ForatTo
).
However my IValidationObject
method isn't returning the ValidationResult
message. I think I need to check the model state in the ForatExcelCompare
method, however I need to be able to return to the previous controller with the error message if the model state is false. Any Ideas?