Hob
Hob

Reputation: 139

How to display selected button as Label

I have years as buttons. Like

2018 2017 2016 2015

I am using a for loop to get the years as buttons.

Index View

@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>
    }
}

Controller

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});
}

Model:

namespace 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; }
}

}

When I click on any year, I am displaying records of that year based on months. I am able to get the month names, not the years. My problem is that I need to display the selected year as a label or heading; if I click 2016 I should see something like:

**2016**
Jan
record 1

record 2

How can I display the clicked year?

Upvotes: 3

Views: 133

Answers (1)

SBFrancies
SBFrancies

Reputation: 4240

Why don't you include it in the Model class:

public class Model
{
    public int Year {get;set;}

    public List<RecordList> Records {get;set;}
}

Then you can set it in the controller:

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});
}

and display it in the view:

<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>
            }
        }

Upvotes: 1

Related Questions