DropDownList包含几周 [英] DropDownList containing weeks

查看:74
本文介绍了DropDownList包含几周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Intranet网站上工作,特别是在针对员工的每周监视工具上.我需要的是用周(例如周)填充 DropDownList :

I'm working on an intranet site, specifically on a Weekly Monitoring tool for employees. What I need is to fill a DropDownList with weeks, example week:

Week : From 12/10/15 to 18/10/15

我可以在其他页面上进行过滤,但可以输入年份和月份:

I'm able to filter on another page but with years and month:

CalendarController c = new CalendarController();
ViewBag.ListYears = c.ListYears(iyear);
ViewBag.ListMonths = c.ListMonths(imonth);
ViewBag.ListDaysOfMonth = _service.ListDaysOfMonth(iyear.ToString(), imonth.ToString());

并使用表格保存它们.

如何在DDList中填充2015年所有星期的列表?

How do I fill my DDList with a list of, let's say, all the weeks in 2015?

推荐答案

要返回格式星期的列表,可以使用以下方法

To return a list of the formatted weeks, you could use the following method

public List<string> FetchWeeks(int year)
{
    List<string> weeks = new List<string>();
    DateTime startDate = new DateTime(year, 1, 1);
    startDate = startDate.AddDays(1 - (int)startDate.DayOfWeek);
    DateTime endDate = startDate.AddDays(6);
    while (startDate.Year < 1 + year)
    {
        weeks.Add(string.Format("Week: From {0:dd/MM/yyyy}to {1:dd/MM/yyyy}", startDate, endDate));
        startDate= startDate.AddDays(7);
        endDate = endDate.AddDays(7);
    }
    return weeks;
}

然后在控制器中

var weeks = FetchWeeks(2105);
ViewBag.Weeks = new SelectList(weeks);

但是,这将回传格式化的值,该值在控制器中可能用处不大,因此可以对其进行修改,以便创建 IEnumerable< SelectListItem> ,其中 Value 属性是代表一年中星期几的数字,而 Text 属性是格式化的文本.

However this will post back the formatted value, which may not be of much use in the controller, so this could be modified so that you create IEnumerable<SelectListItem> where the Value property is a number representing the index of the week in the year and the Text property is the formatted text.

这篇关于DropDownList包含几周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆