albert albert

albert albert

  • NA
  • 524
  • 0

getting week dates of dropdownlist of weeks

May 16 2012 7:40 AM
Hi Everybody,

I have now a dropdownlist of weeks

[code]
 public static IEnumerable<SelectListItem> GetWeekNumbers(this HtmlHelper helper, DateTime allWeeks)
  {
  var janFirst = new DateTime(DateTime.Today.Year + 1, 1, 1);
  //beware different cultures, see other answers
  var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
  return
  Enumerable
  .Range(0, 52)
  .Select(i => new
  {
  weekStart = firstWeek.AddDays(i * 7)
  })
  .TakeWhile(x => x.weekStart.Year <= janFirst.Year)
  .Select(x => new
  {
  x.weekStart,
  weekFinish = x.weekStart.AddDays(6)
  })
  .SkipWhile(x => x.weekFinish < janFirst.AddDays(1))
  .Select((x, i) => new SelectListItem
  {
  Text = new {weekNum = i + 1}.ToString(),
  Value=(i+1).ToString(CultureInfo.InvariantCulture)
  }
 
 
  );

  }//end method
[/code]

working in the view:

[code]
<td> 
  <div class="editor-label">
  @Html.LabelFor(model => model.WeekNumber, "Week")
  </div>
  <div class="editor-field">
 
 
 
  @Html.DropDownListFor(model => model.WeekNumber,Html.GetWeekNumbers(DateTime.Now))
  @Html.ValidationMessageFor(model => model.Week.WeekNumber)
  </div>
  </td>
[/code]

And I have the current week, like this: Monday(17-05-2012), Tuesday(15-05-2012).

But how to get de dates of the week associated with the selecting week of the dropdownlist.

The dates of the week has to be saved in the database.

It is easier to sent the hole project.

So here is the hole project.

THX!!

Answers (10)