2016-03-20 63 views
2

我想生成一個日期列表到Asp.Net MVC5 selectList。我想連續五週開始上榜,但在如何解決這個問題上遇到了實際問題。ASP.Net MVC如何生成日期列表

理想情況下,我需要這個在我創建的ActionMethod中,因爲我想用這個時間來記錄那周的時間。

我一直在嘗試使用下面的例子How can I get the DateTime for the start of the week?並且遇到了困難。

我有什麼是 型號:

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; } 
} 

控制器:創建方法

// 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); 
    } 

請有人可以幫助我還是勸

非常感謝 馬克

回答

2

不知道你究竟是什麼意思「5我們連續「,所以我剛剛做了前5周。完全未經測試,所以如果有任何問題,然後說。

編輯:只有編輯,所以只有下一個5星期一採取。

由於您沒有發佈您嘗試過的內容,因此對於您想要的內容有點含糊不清。

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

感謝@Martin Mazza Dawson我一定會試試這個。澄清連續5周。我想顯示接下來的5周價值日期,即。 周開始 21/03/2016 28/03/2016 2016年4月4日 2016年11月4日 18/04/2016 – markabarmi

+0

@markabarmi編輯這麼名單現在得到今後5個星期。 –

+0

好的,謝謝@Martin。這讓我每天都進入一個很酷的下拉列表。如果我只想讓每週的第一個星期一顯示,我需要做什麼? – markabarmi