[英]How to display selected button as Label
我有多年的按鈕。 喜歡
2018 2017 2016 2015
我正在使用for
循環來獲取按鈕的年份。
@model List<MyRecord.Models.RecordList>
var year = DateTime.Now.Year;
for (var i = year; i > 2012; i--)
{
var j = @i - 1;
<div class="col-md-1 ">
@Html.ActionLink(i.ToString(), "MyPage", new { i = i })
</div>
}
<h3>@ModelYear.Year</h3>
@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>
}
}
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 ModelYear{Records = recordLists, Year = i});
}
模型:
命名空間MyRecord.Models
{
using System;
using System.Collections.Generic;
public partial class RecordList {
public int id { get; set; }
public string title { get; set; }
public Nullable<System.DateTime> date { get; set; }
}
public class ModelYear
{
public int Year { get; set; }
public List<RecordList> Records { get; set; }
}
}
當我點擊任何一年時,我會根據月份顯示當年的記錄。 我能夠得到月份名稱,而不是年份。 我的問題是我需要將選定的年份顯示為標簽或標題; 如果我點擊2016,我應該看到類似的東西:
**2016**
Jan
record 1
record 2
如何顯示點擊的年份?
為什么不將它包含在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>
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.