猿问

如何将所选按钮显示为标签

我有多年的纽扣。喜欢


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;

公共部分类 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

如何显示点击的年份?


喵喵时光机
浏览 218回答 1
1回答

当年话下

为什么不将它包含在 Model 类中:public class Model{&nbsp; &nbsp; public int Year {get;set;}&nbsp; &nbsp; public List<RecordList> Records {get;set;}}然后你可以在控制器中设置它:public ActionResult Archives(int i = 0){&nbsp; &nbsp; var recordLists = new List<RecordList>();&nbsp; &nbsp; &nbsp; if(i == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recordLists = _db.recordlists.Where(p => p.date.Value.Year == DateTime.Now.Year)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderByDescending(p => p.date).ToList();&nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recordLists = _db.recordlists.Where(p => p.date.Value.Year == i).OrderByDescending(p => p.date).ToList();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; 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 }))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3 class="monthHeader"> @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(groupMonth.Key.Month)</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var recordLists in groupMonth)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.Partial("_PartialView", recordList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答