I am trying to add all days from a date range in my database but I couldn't find a way to do that
I am getting the data range from View with
but to add I could only test for one value
public async Task OnPostAddNewAsync(DateTime sdate, DateTime edate)
{
var newData = new CMPAvDate()
{
CMPDate = sdate
};
if (await TryUpdateModelAsync(newData, "CMPAvDate"))
{
_context.CMPAvDates.Add(newData);
await _context.SaveChangesAsync();
return RedirectToPage("/CMP/CMPSchedule", new {sdate});
}
return RedirectToPage("/CMP/CMPSchedule");
}
I also tried without success
public static IEnumerable EachCalendarDay(DateTime sdate, DateTime edate)
{
for (var date = sdate.Date; date.Date <= edate.Date; date = date.AddDays(1)) yield
return date;
}
foreach (DateTime day in EachCalendarDay(sdate, edate))
{
var newData = new CMPAvDate()
{
CMPDate= day
};
if (await TryUpdateModelAsync(newData, "CMPAvDate"))
{
_context.CMPAvDates.Add(newData);
await _context.SaveChangesAsync();
return RedirectToPage("/CMP/CMPSchedule");
}
} 
Marius VasilePosted Jan 23, 2023, 9:39 AM
I removed () after catch and it worked. Thank you Sachin
Sachin SinghPosted Jan 23, 2023, 9:32 AM
have you tested it?
with try catch and return outside the foreach ?
Sachin SinghPosted Jan 23, 2023, 9:31 AM
test this
Marius VasilePosted Jan 23, 2023, 9:24 AM
This method will only add one date but not the range