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

1 Answer 1

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"})
Sign up to request clarification or add additional context in comments.

4 Comments

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 edited it so list gets next 5 weeks now.
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.