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