1 回答
TA贡献1890条经验 获得超9个赞
为什么不将它包含在 Model 类中:
public class Model
{
public int Year {get;set;}
public List<RecordList> Records {get;set;}
}
然后你可以在控制器中设置它:
public ActionResult Archives(int i = 0)
{
var recordLists = new List<RecordList>();
if(i == 0)
recordLists = _db.recordlists.Where(p => p.date.Value.Year == DateTime.Now.Year)
.OrderByDescending(p => p.date).ToList();
else{
recordLists = _db.recordlists.Where(p => p.date.Value.Year == i).OrderByDescending(p => p.date).ToList();
}
return View(new Model{Records = recordLists, Year = i});
}
并将其显示在视图中:
<h1>@Model.Year</h1>
@foreach (var groupMonth in Model.Records.GroupBy(recordLists => new { recordLists.date.Value.Year, recordLists.date.Value.Month }))
{
<h3 class="monthHeader"> @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(groupMonth.Key.Month)</h3>
foreach (var recordLists in groupMonth)
{
<div class="row">
@Html.Partial("_PartialView", recordList)
</div>
}
}
- 1 回答
- 0 关注
- 217 浏览
添加回答
举报