简体   繁体   中英

ASP.Net MVC How to generate a list of dates

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

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"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM