I have written several custom validation classes that permit comparing pairs of dates to see if they are valid, e.g. end date on or after start date. See mvc4 data annotation compare two dates. These work well when used within a single model. But I don’t know how to use them in a view model of parent and child entities to be used for creating and editing the child entities.
Let the parent entity be an Academic Calendar and the child entity be a Course. I am trying to create or edit a Course. In addition to requiring the Course start and end dates to be valid, I need to be able to check that the Course start date is on or after the Calendar start date and that the Course end date is on or before the Calendar end date. So I need to compare dates across two data models.
I created a view model that includes both the course data and calendar data, e.g. CourseId, CourseName, CourseStartDate, CourseEndDate plus CalendarId, CalendarName, CalendarStartDate, and CalendarEndDate. In the view, I can create a dropdown for Calendars with value and text fields corresponding to id and name, e.g. 1 and Fall 2018. My problem is that I need to be able to show the corresponding Calendar Start and End dates corresponding to the selected calendar. With Web Forms, I could add extra hidden columns to a dropdown, but I don’t know how to do that with MVC and Razor. I’d like the Calendar dates to be in read-only textboxes, so they can be checked by my comparison validation classes before attempting to insert a new ─ or update an existing ─ course. I need these Calendar date values to change when the selection changes in the dropdown.