如何在C#中创建一个Dropdownlist - MVC3 with all Month [英] How can i create a Dropdownlist in C# - MVC3 with all Month

查看:74
本文介绍了如何在C#中创建一个Dropdownlist - MVC3 with all Month的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是初学者,有一个问题我怎样才能创建一个包含所有月份的下拉列表,

也是当前月份的选定选项。



我需要在C#中使用 - MVC3用于以下网格控制器:

Hello,

i'm a beginner and have a question how can i create a Dropdownlist with all Month,
also with the selected option of the current month.

I need that in C# - MVC3 for the follow Grid Controller:

public ActionResult Index()
        {
            DateTime startdate = DateTime.Today.AddDays((DateTime.Today.Day - 1) * -1);
            DateTime enddate = startdate.AddMonths(1).AddDays(-1);
            var data = new RosterIndexViewModel(startdate.Year, startdate.Month);
            data.GridRows = (from e in db.EmployeeSet
                             select new RosterGridRow() { Employee = e }).ToList();
            // Diensteinzeilung für gwähltes Monat laden
            foreach (var gr in data.GridRows)
            {
                gr.RosterEntries = (from r in db.RosterSet
                                    where r.EmployeeId == gr.Employee.Id
                                        && r.RosterDate >= startdate && r.RosterDate <= enddate
                                    select r).ToList();
            }





我会感谢您的所有提示。



谢谢



Bernhard



I would be thankful for all your tips.

Thank you

Bernhard

推荐答案

在你的aspx文件中,输入以下行。如果您使用剃刀视图引擎,请调整它。



我的代码也是一个独立的下拉列表,基于MVC 2.希望它无论如何都可以帮助。

In your aspx file, put the following line. Adjust it if you are using razor view engine.

My code is also for a stand-alone drop down list and is based on MVC 2. Hope it can help anyway.
<% Html.DropDownFor(model => model.SelectedMonth, Model.MonthList, "Please select the month"); %>





您的模型将类似于:



You model will look something like:

public class Model
{
    public int? SelectedMonth { get; set; };

    public SelectList MonthList
    {
        get
        {
            return new SelectList(AllMonths, "Index", "Name", SelectedMonth);
        }
    }

    public static IEnumerable<MonthInfo> AllMonths
    {
        get
        {
            CultureInfo info = 
               System.Threading.Thread.CurrentThread.CurrentCulture;

            int index = 1;
            foreach (var monthName in info.DateTimeFormat.MonthNames)
            {
                yield return new MonthInfo
                {
                    Index = index,
                    Name = monthName,
                };
                ++index;
            }
        }
    }

    public class MonthInfo
    {
        public int Index { get; set; }
        public string Name { get; set; }
    }
}


谢谢Philippe,



现在我只有一个问题我如何连接datagrid = table输出的startdate和enddate。

Thanks Philippe,

now i have only one problem how i connect that with the startdate and enddate of the datagrid = table output.
DateTime startdate = DateTime.Today.AddDays((DateTime.Today.Day - 1) * -1);
DateTime enddate = startdate.AddMonths(1).AddDays(-1);





使用DropdownList,我想从所选月份中选择所有数据库条目的表输出月份。 />


我认为我必须将AddMonth(1)的1更改为参数,我得到对MonthList函数的引用,但我怎么能得到这个?



这个组合的第二个问题是startdate。



谢谢!



With the DropdownList i want choose the Month of the Table Output of all Database Entries from the selected month.

I think that i have to change the "1" of AddMonth(1) to a parameter with that i get a reference to MonthList Function, but how can i get this?

The second problem in this combination is the startdate.

Thank you!


您可以在 AllMonths 属性中过滤几个月(假设您只想处理每年12个月的日历):



Well you can filter months in AllMonths property with something like that (assuming that you only want to handle calendars with 12 months each year) :

int startYear = startdate.Year;
int startMonth = startdate.Month;

int index = 1;
foreach (var monthName in info.DateTimeFormat.MonthNames)
{
    var testYear = index < startMonth ? startYear + 1 : startYear
    var testDate = new DateTime(testYear, index, 1);
    
    if (testDate >= startDate && testDate < enddate)
    {
        yield return new MonthInfo { Index = index, MonthName = monthName };
    }
    ++index;
}





你必须删除 static 模型中的修饰符和存储开始和结束日期。



You will have to remove static modifier and store start and end date in your model.


这篇关于如何在C#中创建一个Dropdownlist - MVC3 with all Month的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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