亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將所選按鈕顯示為標簽

如何將所選按鈕顯示為標簽

C#
喵喵時光機 2021-10-24 16:56:06
我有多年的紐扣。喜歡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**Janrecord 1record 2如何顯示點擊的年份?
查看完整描述

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>

            }

        }


查看完整回答
反對 回復 2021-10-24
  • 1 回答
  • 0 關注
  • 241 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號