So I've created a function that navigates through every week and displays the timereports data in database, the problem is that it can only navigate once and not further, I need some help.
Model:
using System;
using System.Globalization;
namespace ReportSystem.ViewModel
{
public static class WeekConverter
{
public static DateTime FirstDateOfWeek(int year, int weekOfYear)
{
var jan1 = new DateTime(year, 1, 1);
var daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
var firstThursday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
var firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = weekOfYear;
if (firstWeek <= 1)
{
weekNum -= 1;
}
var result = firstThursday.AddDays(weekNum * 7);
return result.AddDays(-3);
}
public static int GetIso8601WeekOfYear(DateTime time)
{
var day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
}
}
View:
@model IEnumerable<ReportSystem.Models.TimeReports>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Week: @((DateTime)ViewBag.Date)</h2>
<p>
@Html.ActionLink("Create New", "Create", "TimeReports") |
@Html.ActionLink("Create New Day Off", "Index", "Other")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Project.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Consultants.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.TotalHours)
</th>
<th>
@Html.DisplayNameFor(model => model.TimeType)
</th>
<th>
@Html.DisplayNameFor(model => model.OnSite)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Project.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Consultants.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalHours)
</td>
<td>
@Html.DisplayFor(modelItem => item.TimeType)
</td>
<td>
@Html.DisplayFor(modelItem => item.OnSite)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("Previous Week", "PreviousWeek", "TimeReports") |
@Html.ActionLink("Next Week", "NextWeek", "TimeReports")
</p>
Controller:
using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using ReportSystem.Models;
using ReportSystem.ViewModel;
namespace ReportSystem.Controllers
{
public class TimeReportsController : Controller
{
private readonly ReportsDatabaseEntities _db = new ReportsDatabaseEntities();
public ActionResult Index()
{
var weekNr = WeekConverter.GetIso8601WeekOfYear(DateTime.Now);
var startTime = WeekConverter.FirstDateOfWeek(DateTime.Now.Year, weekNr);
var endTime = startTime.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59);
var consultantId = (int)Session["Id"];
var timeReports = _db.TimeReports.Where(s => s.Date >= startTime && s.Date <= endTime)
.Where(s => s.ConsultantID == consultantId).ToList();
ViewBag.Date = startTime;
return View(timeReports);
}
public ActionResult PreviousWeek()
{
var weekNr = WeekConverter.GetIso8601WeekOfYear(DateTime.Now); //The value keeps staying at same week...
var startTime = WeekConverter.FirstDateOfWeek(DateTime.Now.Year, weekNr);
startTime = startTime.AddDays(-7);
var endTime = startTime.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59);
var consultantId = (int)Session["Id"];
var timeReports = _db.TimeReports.Where(s => s.Date >= startTime && s.Date <= endTime)
.Where(s => s.ConsultantID == consultantId).ToList();
ViewBag.Date = startTime;
return View("Index", timeReports);
}
public ActionResult NextWeek()
{
var weekNr = WeekConverter.GetIso8601WeekOfYear(DateTime.Now);//The value keeps staying at same week...
var startTime = WeekConverter.FirstDateOfWeek(DateTime.Now.Year, weekNr);
startTime = startTime.AddDays(7);
var endTime = startTime.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59);
var consultantId = (int)Session["Id"];
var timeReports = _db.TimeReports.Where(s => s.Date >= startTime && s.Date <= endTime)
.Where(s => s.ConsultantID == consultantId).ToList();
ViewBag.Date = startTime;
return View("Index", timeReports);
}
Any help would be appreciated.