albert albert

albert albert

  • NA
  • 524
  • 0

How to add a extension method with razor?

May 15 2012 2:56 PM
HI Everyone,

I have this:

[code]

public static class DateTimeExtensions

 public static DateTime GetWeekNumbers(this HtmlHelper helper, DateTime allWeeks)
        {
            var jan1 = new DateTime(DateTime.Today.Year + 1, 1, 1);
            //beware different cultures, see other answers
            var startOfFirstWeek = jan1.AddDays(1 - (int)(jan1.DayOfWeek));
            var weeks =
                Enumerable
                    .Range(0, 52)
                    .Select(i => new
                    {
                        weekStart = startOfFirstWeek.AddDays(i * 7)
                    })
                    .TakeWhile(x => x.weekStart.Year <= jan1.Year)
                    .Select(x => new
                    {
                        x.weekStart,
                        weekFinish = x.weekStart.AddDays(6)
                    })
                    .SkipWhile(x => x.weekFinish < jan1.AddDays(1))
                    .Select((x, i) => new
                    {
                        x.weekStart,
                        x.weekFinish,
                        weekNum = i + 1
                    });

            return allWeeks;
        }//end method


[/code]

and I want to have it in a dropdwonlist, like this:

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

But how to call theextension method in the view? it is about this line: @Html.DropDownListFor(GetWeekNumbers(),"hallo")

THX

Answers (2)