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]
@Html.LabelFor(model => model.WeekNumber, "Week")
@Html.DropDownListFor(GetWeekNumbers(),"hallo")
@Html.ValidationMessageFor(model => model.Week.WeekNumber)
[/code]
But how to call theextension method in the view? it is about this line: @Html.DropDownListFor(GetWeekNumbers(),"hallo")
THX
albert albertPosted May 16, 2012, 4:05 AM
i have it now like this:
[code]
@using ManyToMany.Helpers
@using ManyToMany.Controllers
@model ManyToMany.Models.Hour
@{
ViewBag.Title = "Create";
}
Create
@{
HtmlHelper allWeeks;
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
}
@Html.ActionLink("Back to List", "Index")
[code]
But I get this error:
[code]
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor
Source Error:
Source File: c:\Users\n.engelen\Documents\Visual Studio 2010\Projects\projects16052012\winitu16052012\ManyToMany\ManyToMany\Views\Hour\Create.cshtml Line: 53
[/code]
I have also attached the hole project
Posted May 15, 2012, 11:39 PM
Please try something like this.
First include the namespace in the cshtml page and you should be able to get the extension method.