0

I have two date on my razor view and I need to valdiate on that and using data annotation show the error if To date is less than from date. when I run the code in the debug mode the break point hits but it is not rendering the error on the page. it displays the form again the same way if the model state is not valid Below is my code, am I doing something wrong.

public class AcctViewModel: IValidatableObject
{

 [Key] public long AccountId { get; set;}

  [Display(Name = "Account Name"), DataType(DataType.Text), StringLength(15), Required]
  public string AcctName {get;set;}
   
   [Display(Name = "From Date"), DataType(DataType.Date), Required]
   public DateTime FromDate { get; set; }

   [Display(Name = "To Date"), DataType(DataType.Date), Required]
   public DateTime ToDate { get; set; }


   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errors = new List<ValidationResult>();
            if (ToDate < FromDate)
            {
                errors.Add(new ValidationResult($"{nameof(ToDate)} needs to be greater than From date.", new List<string> { nameof(ToDate) }));
            }
            return errors;
        }

}

 <div class="form-row">
                        <div class="col-md-12">
                            <div class="form-group form-group--float">
                                <input class="form-control date-picker" asp-for="ToDate">
                                <label asp-for="ToDate"></label>
                              
                                <div class="invalid-tooltip"><span asp-validation-for="ToDate"></span></div>
                                <i class="form-group__bar"></i>
                            </div>
                        </div>
                    </div>

                </div>
3
  • 1
    i test you code. and it's work well. maybe problem is in view . Commented Jul 13, 2020 at 6:52
  • can you share your view? Commented Jul 13, 2020 at 13:06
  • I also tested the custom Validate method, it works well on my side. According to your code (` <input class="form-control date-picker" asp-for="ToDate">`), it seems that you are using datepicker plugin, right? If that is the case, try to remove it and recheck whether the code works well (you could also refer foadabd's sample). If it works, we can sure that the issue is related the datepicker plugin, If still not working, can you post enough code or create a simple sample to reproduce the problem? Commented Jul 14, 2020 at 8:51

1 Answer 1

1

Controller:

 public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Index(AcctViewModel model)
        {
            if (ModelState.IsValid)
            {

            }
            return View(model);
        }

View:

   <form asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="AccountId" class="control-label"></label>
                <input asp-for="AccountId" class="form-control" />
                <span asp-validation-for="AccountId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="AcctName" class="control-label"></label>
                <input asp-for="AcctName" class="form-control" />
                <span asp-validation-for="AcctName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FromDate" class="control-label"></label>
                <input asp-for="FromDate" class="form-control" />
                <span asp-validation-for="FromDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ToDate" class="control-label"></label>
                <input asp-for="ToDate" class="form-control" />
                <span asp-validation-for="ToDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>

model:

   public class AcctViewModel : IValidatableObject
    {
        [Key] 
        public long AccountId { get; set; }

        [Display(Name = "Account Name"), DataType(DataType.Text), StringLength(15), Required]
        public string AcctName { get; set; }

        [Display(Name = "From Date"), DataType(DataType.Date), Required]
        public DateTime FromDate { get; set; }

        [Display(Name = "To Date"), DataType(DataType.Date), Required]
        public DateTime ToDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errors = new List<ValidationResult>();
            if (ToDate < FromDate)
            {
                errors.Add(new ValidationResult($"{nameof(ToDate)} needs to be greater than From date.", new List<string> { nameof(ToDate) }));
            }
            return errors;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.