I've got a little issue that's bugging me today!! I've created a little helper method that I want to ALWAYS return monday as the 1st day of week (i.e. monday=0) but can't 开发者_如何学Cseem to figure out where i'm going wrong. I'm setting what I 'feel' is an override of the prevailing culture but to no avail.
Without further ado, the code:
public static string ComboDaysOfWeekNumber(this HtmlHelper helper,
string id, string selectedValue)
{
var cultureInfo = new DateTimeFormatInfo();
cultureInfo.FirstDayOfWeek = DayOfWeek.Monday;
var newitems = cultureInfo
.DayNames
.Select((dayName, index) => new SelectListItem
{
Value =(index).ToString(),
Text = dayName,
Selected = (selectedValue == dayName)
});
var result = helper.DropDownList(id, newitems).ToHtmlString();
return result;
}
usage:
<%=Html.ComboDaysOfWeekNumber("weekSplitEnd", myModelDayNo.ToString())%>
No matter what, this is ALWAYS starting on sunday=0.
any thoughts out there??
DayNames
is not dependent on the FirstDayOfWeek
property and setting it won't have any effect. You could try this instead:
var cultureInfo = new DateTimeFormatInfo();
var dayNames = new[]
{
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday,
DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday,
DayOfWeek.Sunday
}.Select(cultureInfo.GetDayName);
var newitems = dayNames
.Select((dayName, index) => new SelectListItem
{
Value =(index).ToString(),
Text = dayName,
Selected = (selectedValue == dayName)
});
var result = helper.DropDownList(id, newitems).ToHtmlString();
return result;
Darin,
back now, here's what I came up woth:
public static MvcHtmlString ComboDaysOfWeekNumber(this HtmlHelper helper,
string id, int selectedValue)
{
var dayNames = new[]
{
DayOfWeek.Monday, DayOfWeek.Tuesday,
DayOfWeek.Wednesday, DayOfWeek.Thursday,
DayOfWeek.Friday, DayOfWeek.Saturday,
DayOfWeek.Sunday
};
var newitems = dayNames
.Select((dayName, index) => new SelectListItem
{
Value =(index).ToString(),
Text = dayName.ToString(),
Selected = (selectedValue == index)
});
var result = helper.DropDownList(id, newitems);
return result;
}
this 'seems' to work as expected. thanks for the pointers.
jim
精彩评论