【发布时间】:2018-10-25 13:28:58
【问题描述】:
我有返回日期列表的模型函数。在我看来,我不想将类似的代码复制到 jQuery 中来填充下拉列表。
jQuery 可以执行它在 C# 中的模型函数并存储日期列表吗?
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;
}
以下是我现在所做的。但我想通过不在 jQuery 中创建类似的代码来减少代码重复。因此 jQuery 需要执行 C# 函数并将日期列表存储在变量中,然后填充到下拉列表中。
<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>
【问题讨论】:
-
如何做一个返回 GetAvailableDateInStrings 函数的 JsonResult 的操作。然后让您的 jQuery 调用该操作并遍历它返回的 Json 集合以填充您的选择。
-
“jQuery 能否执行它在 C# 中的模型函数”...如果您通过操作方法公开它,那么可以。这种东西就是 AJAX 的用途。
标签: c# jquery asp.net-mvc