我正在运行asp.net 4 mvc,并且我创建了默认为列表中第一个条目的日期的DropDownList。当我选择一个条目时,我调用一个控制器函数并做一些处理。但是,当我的页面进行回发时,不是显示我选择的列表项目,而是再次显示原始默认列表项目。DropDownList未设置选定的值
如何让我的页面显示我从列表中选择的最后一个项目?我花了整整两天的时间搜索这个网站和互联网寻找解决方案,但我没有尝试似乎工作。任何援助将不胜感激。
我的HTML视图
@Html.DropDownList("selectList", Model.ReverseMonthsLists(),
new { @onchange = "CallChangefunc(this.value)" })
<script>
function CallChangefunc(val) {
window.location.href = "/dashboard/Report_Performance?id=" + val;
}
</script>
我的视图模型
public SelectList MonthList { get; set; }
public IEnumerable<SelectListItem> ReverseMonthsLists()
{
var selectListItems = GetDates()
.Select(_ => _.ToString("MMM yyyy"))
.Select((dateString, index) => new SelectListItem { Selected = index == 0, Text = dateString, Value = dateString })
.ToList();
return selectListItems;
}
public static IEnumerable<DateTime> GetDates()
{
DateTime startDate = new DateTime(2017, 6, 1).Date;
var currentDate = DateTime.Now.Date;
int numberOfMonthsToShow = (currentDate.Year - startDate.Year) * 12 + currentDate.Month - startDate.Month;
var dates = new List<DateTime>(numberOfMonthsToShow);
currentDate = currentDate.AddMonths(-1);
for (int i = 0; i < numberOfMonthsToShow; i++)
{
dates.Add(currentDate);
currentDate = currentDate.AddMonths(-1);
}
return dates;
}
我的控制器
[RequireLogin]
public ActionResult Report_Performance(string id)
{
DateTime newDate = DateTime.Now.Date.AddMonths(-1);
if (id != null)
newDate = DateTime.Parse(id);
var aVar = Models.Reporting.ListingStatsReportingViewModel.GetStats(userCurrentService.CompanyId.Value, Models.Reporting.DateTimePeriod.Monthly, newDate);
return this.View(aVar);
}
能告诉你的'GetStats'方法的代码?我猜测它总是返回相同的数据,所以它会帮助我们看到底下发生了什么。 – sleeyuen