2

I am trying to generate a list of dates into a selectList in Asp.Net MVC5. I would like to have week commencing list for only 5 weeks in a row but am hitting real problems on how to go about this.

I ideally I would need this in my create ActionMethod because I want to use this against time recorded for that week.

I have been trying to use the following example How can I get the DateTime for the start of the week? and am running into difficulties.

What I have is Model:

public class TimeSheet
{
    public int TimeSheetId { get; set; }
    public DateTime WeekCommencing { get; set; }
    public int MondayHours { get; set; }

    public int TuesdayHours { get; set; }

    public int WednesdayHours { get; set; }

    public int ThursdayHours { get; set; }

    public int FridayHours { get; set; }

    public int SaturdayHours { get; set; }

    public int SundayHours { get; set; }
    public bool CompletedTimeSheet { get; set; }
    public int PlanId { get; set; }

    public virtual ICollection<Plan> Plan { get; set; }
}

Controller: Create Method

  // GET: TimeSheets/Create
    public ActionResult Create()
    {
        DateTime today = DateTime.Today;
        if(today.DayOfWeek == DayOfWeek.Monday && today.Day <= 7)
            ViewBag
        return View();
    }

    // POST: TimeSheets/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "TimeSheetId,WeekCommencing,MondayHours,TuesdayHours,WednesdayHours,ThursdayHours,FridayHours,SaturdayHours,SundayHours,CompletedTimeSheet,PlanId")] TimeSheet timeSheet)
    {
        if (ModelState.IsValid)
        {
            db.TimeSheets.Add(timeSheet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(timeSheet);
    }

Please can someone help me or advise

Many thanks Mark

Community
  • 1
  • 1
markabarmi
  • 245
  • 1
  • 14

1 Answers1

2

Not sure exactly what you mean by "5 weeks in a row", so I just did previous 5 weeks. Completely untested, so if any problems then say.

Edit: Edited so only next 5 Mondays get taken.

It's a bit ambiguous as to what you want as you haven't posted what you have tried.

public class TimeSheet
{
    public DateTime DateSelected { get; set; }
}

public ActionResult Create()
{
    int weekCount = 5;
    List<DateTime> listDates = new List<DateTime>();

    for (int i = 0; i < (weekCount * 7); ++i) //Get next 5 weeks
    {
        //Adds only next 5 mondays to the list of dates
        if (DateTime.Today.AddDays(i).DayOfWeek == DayOfWeek.Monday)
                listDates.Add(DateTime.Today.AddDays(i));
    }

    ViewData["DateList"] = new SelectList(listDates);       

    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "TimeSheetId,WeekCommencing,MondayHours,TuesdayHours,WednesdayHours,ThursdayHours,FridayHours,SaturdayHours,SundayHours,CompletedTimeSheet,PlanId,DateSelected")] TimeSheet timeSheet)
{
    if (ModelState.IsValid)
    {
        db.TimeSheets.Add(timeSheet);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(timeSheet);
}

@Html.DropDownListFor(x => x.DateSelected, (SelectList)ViewData["DateList"], new {@class = "form-control"})
@Html.ValidationMessageFor(x => x.DateSelected, "", new {@class = "text-danger"})
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
  • thanks @Martin Mazza Dawson I will certainly will give this a try. To clarify the 5 week in a row. I wanted to display the next 5 weeks worth of dates ie. week commencing 21/03/2016 28/03/2016 04/04/2016 11/04/2016 18/04/2016 – markabarmi Mar 20 '16 at 18:11
  • @markabarmi edited it so list gets next 5 weeks now. – Martin Dawson Mar 20 '16 at 18:20
  • Ok thanks @Martin. This gives me all the days into a dropdownlist which is cool. What would I need to do if I just wanted to have the first Monday of each week to be displayed ? – markabarmi Mar 20 '16 at 18:24
  • @markabarmi Edited for only next 5 Mondays. If the dates are in the wrong format, then just do a quick google search for 'Convert DateTime to a specified Format' and then pass them into the list. – Martin Dawson Mar 20 '16 at 18:40