我没有在表单提交时收到验证错误,而是显示控制器设置的通用错误页面。
控制器:
public ActionResult Add(Loan loan)
{
try
{
_dbLoan.Loans.Add(loan);
_dbLoan.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View("Error");
}
}
部分视图:
<div class="form-group input-group-sm">
@Html.LabelFor(model => model.DeliveryDate)
@Html.TextBoxFor(model => model.DeliveryDate, new { @class = "form-control datepicker", placeholder = "Enter Delivery date here DD-MM-YYYY..." })
@Html.ValidationMessageFor(model => model.DeliveryDate)
</div>
型号:
public class Loan : IValidatableObject
{
/// <summary>
/// Instance Variables
/// </summary>
[Key]
public int LoanID { get; set; }
[Display(Name = "Select Date")]
[Required]
[DataType(DataType.Date)]
public DateTime DeliveryDate { get; set; }
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
List<ValidationResult> res = new List<ValidationResult>();
if (DeliveryDate < DateTime.Today)
{
ValidationResult mss = new ValidationResult("Appointment date must not be in the past. Please try again.");
res.Add(mss);
}
return res;
}
任何人都可以帮助解决为什么忽略验证错误? 感谢。