Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
You have already asked this a few hours earlier here. Please delete one or the other. –  Stephen Muecke Mar 19 at 22:39

2 Answers 2

Assuming the initial view renders a collection of time reports for the current week, and you want links to redirect to a view that renders a collection of reports for the next/previous weeks, then you need one method that has a parameter identifying the week. Dealing with a DateTime property can be problematic, so for this example I'll use an int identifying the week relative to the current week

public ActionResult Index(int ID = 0)
{
  DateTime firstOfWeek = DateTime.Today.AddDays(ID * 7).FirstOfWeek() // see notes below
  DateTime lastofWeek = firstOfWeek.AddDays(7).AddSeconds(-1);
  var consultantId = (int)Session["Id"];
  var timeReports = _db.TimeReports.Where(s => s.Date >= firstOfWeek && s.Date <= lastofWeek).Where(s => s.ConsultantID == consultantId); // don't need .ToList()
  ViewBag.PreviousWeek = ID - 1; // better to use a view model rather than ViewBag
  ViewBag.NextWeek = ID + 1; 
  return View("Index", timeReports);
}

Then in the view

@model IEnumerable<ReportSystem.Models.TimeReports>
<table>
  ....
</table>
@Html.ActionLink("Previous Week", "Index", "TimeReports", new { ID = ViewBag.PreviousWeek })
// Assume you don't want to navigate to a future week
@if (ViewBag.NextWeek < 0)
{
  @Html.ActionLink("Next Week", "Index", "TimeReports" new { ID = ViewBag.NextWeek })
}

I cant understand what that code in your WeekConverter class is supposed to be doing. If you want to get the first day of the week then create an simple extension method

// Extension method to return the first  day of the week (Sunday).
public static DateTime FirstOfWeek(this DateTime date)
{
  return date.AddDays(DayOfWeek.Sunday - date.DayOfWeek);
}
// Extension method to return the first working day of the week (Monday).
public static DateTime FirstOfWorkingWeek(this DateTime date)
{
  return date.AddDays(DayOfWeek.Monday - date.DayOfWeek);
}
share|improve this answer

this ActionResult "NextWeek()" need started week parameter or else a week as the same all the time (DateTime.Now)

create map routes with parameter i'm not shure but look this to build map root

routes.MapRoute("Default", "{controller}/{action}/{id}",
    new {controller = "TimeReports", 
    action = "PreviousWeek", 
    WeekStart = UrlParameter.Optional})

look this for help to build map routes ASP.net mvc ActionResult parameter

share|improve this answer
    
How is the parameter supposed to look? –  Andre Korosh Kordasti Mar 19 at 15:13
    
in view something like this @Html.ActionLink("Previous Week", "PreviousWeek", "TimeReports", new { WeekStart [email protected] }); and in the model ActionResult PreviousWeek( DateTime WeekStart ) –  Eric Siodmak Mar 19 at 16:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.