I am developing an application in MVC using Entity Framework and C#. I want to display all my cases based on month when I click on year. To filter years I am running for loop to get the years from 2009 to current year and displaying years like button. By default or when I first enter the page I should be able to see the current year details. When I click on any year, the page should refreshed and get all the cases of each month for that clicked year. If i click 2017 I should get only the months of 2017 which have cases.
For simple Understanding all my years on the top are like button. When I click on any year i should populate that years data.
Something like this:
View:
@model List<Magazine.Models.Case>
@{
ViewBag.Title = "CaseList";
}
<div>
<div>
<div class="container">
<div>
@{
var year = DateTime.Now.Year;
for (var i = year; i > 2009; i--)
{
<div class="col-md-1 ">
@Html.ActionLink(i.ToString(), "MyCases/" + i, new { onclick="$('.container').hide(); $('.container.i').show();" })
</div>
}
}
</div>
</div>
<div class="container">
@foreach (var groupMonth in Model.GroupBy(case => new { case.date.Value.Year, case.date.Value.Month }))
{
<h3> @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(groupMonth.Key.Month)</h3>
foreach (var case in groupMonth)
{
<div class="row">
@case.date
<div>
@case.title
</div>
<div>
@case.description
</div>
</div>
}
}
</div>
Controller :
public ActionResult MyCases(int year = 0)
{
var cases = _db.case.Where(p => p.date.Value.Year >= 2009 &&
p.pdate.Value.Year <= DateTime.Now.Year)
.OrderByDescending(p => p.date).ToList();
return View(cases);
}
Right now in my code i have just added the years loop and added the onclick for that but it it not filtering the data based on year. I need to get the clicked year value and pass it foreach loop to display the clicked year details. How do I do that?