albert albert

albert albert

  • NA
  • 524
  • 0

filter days of selected week

May 16 2012 11:11 AM
Hi everyone,

I have this:

[code]
 public static IEnumerable<SelectListItem> GetWeekNumbers(this HtmlHelper helper, DateTime allWeeks)
        {
              var janFirst = new DateTime(DateTime.Today.Year, 1, 1);
              //beware different cultures, see other answers
              var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
            int j = -1;
            foreach (var x1 in Enumerable.Range(0, 52).Select(i => new
                                                                       {
                                                                           weekStart = firstWeek.AddDays(i*7),
                                                                           Tuesday = firstWeek.AddDays(i * 3)
                                                                       }).TakeWhile(x => x.weekStart.Year <= janFirst.Year).Select(x => new
                                                                                                                                            {
                                                                                                                                               x.weekStart,

                                                                                                                                               tuesday = x.weekStart.AddDays(3),
                                                                                                                                               weekFinish = x.weekStart.AddDays(6)
                                                                                                                                            }).SkipWhile(x => x.weekFinish < janFirst.AddDays(1)))
            {
                j++;
                yield return new SelectListItem
                                 {
                                     Text = new { maandag = firstWeek.AddDays(j * 7).ToShortDateString(), Dindsdag = x1.weekStart.AddDays(1) .ToShortDateString(),
                                         vrijdag = x1.weekStart.AddDays(6).ToShortDateString(), weekNum = j + 1 }.ToString(),
                                     Value = (j + 1).ToString(CultureInfo.InvariantCulture)
                                 };
            }
        }//end method
[/code]

and for the view:

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

now how can I get the first day( weekStart = firstWeek.AddDays(i*7)) = actualy every  Monday of a week.

in a seperate TextBoxFor, lilke this;

[code]
 <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Monday, "Maandag")
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.Monday, Html.GetWeekNumbers(DateTime.UtcNow))
                        @Html.ValidationMessageFor(model => model.Monday)
                    </div>
                </td>
[/code]

THX!!

Answers (1)