I have model function that returns a list of dates. In my view, I don't want to duplicate the similar code into jQuery to populate a dropdown list.
Can the jQuery execute the model function which it's in C# and store the list of dates?
public List<string> GetAvailableDateInStrings()
{
List<string> dateList = new List<string>();
foreach(Year year in this.m_years)
{
foreach(Month month in year.GetMonths())
{
string monthString = month.MonthInEnum.ToString() + " " + year.CalendarYear.ToString();
if(year.CalendarYear == this.m_cutOffYear && month.MonthInYear <= this.m_cutOffMonth)
{
dateList.Add(monthString);
}
else if(year.CalendarYear < this.m_cutOffYear)
{
dateList.Add(monthString);
}
}
}
return dateList;
}
Below is what I do now. But I want to reduce the code duplication by not creating the similar code in jQuery. So the jQuery needs to execute the C# function and store the list of dates in variable, then populate into dropdown list.
<script src="/Bootstrap_Sufee/assets/js/vendor/jquery-2.1.4.min.js"></script>
<script type="text/javascript">$(document).ready(function() {
$("#attendanceReportType").change(function() {
if ($("#attendanceReportType").val() == "Class Monthly") {
$("#attendanceReportYear").empty();
$("#attendanceReportYear").append("<option>May 2018</option>");
$("#attendanceReportYear").append("<option>April 2018</option>");
$("#attendanceReportYear").append("<option>March 2018</option>");
});
});</script>