Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
            }


I would be thankful for all your tips.

Thank you

Bernhard
Posted

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:
C#
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; }
    }
}
 
Share this answer
 
v2
Comments
fjdiewornncalwe 9-Jul-11 10:41am    
Nicely done.
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);


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!
 
Share this answer
 
Comments
Philippe Mori 9-Jul-11 9:21am    
By the way, you can also write:
Uses a temporary variable to ensure the date won't change between multiple call if the first call is just before midnight.
var today = DateTime.Today;
The following code is a bit cleared that we want the first day of the month.
startDate = new DateTime(today.Year, today.Month, 1);
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;
}


You will have to remove static modifier and store start and end date in your model.
 
Share this answer
 
Thanks :)

Now i did entered the data such as follow.

My ViewModel:
// MonthList-Model for Dropdownlist
        public int? SelectedMonth;

        public SelectList MonthList
        {
            get
            {
                return new SelectList(AllMonths, "Index", "Name", SelectedMonth);
            }
        }
        public IEnumerable<MonthInfo> AllMonths
        {
            get
            {
                CultureInfo info =
                    System.Threading.Thread.CurrentThread.CurrentCulture;

                var today = DateTime.Today;

                DateTime startDate = new DateTime(today.Year, today.Month, 1);
                DateTime endDate = startDate.AddMonths(1).AddDays(-1);

                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, Name = monthName };
                    }
                    ++index;
                }

            }
        }

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

         //---- End DropdownList


My Controller:
C#
public ActionResult Index()
{

    //DateTime startdate = DateTime.Today.AddDays((DateTime.Today.Day - 1) * -1);
    //DateTime enddate = startdate.AddMonths(1).AddDays(-1);
    var data = new RosterIndexViewModel(startYear, startMonth);
    //var data = new RosterIndexViewModel(startdate.Year, startdate.Month);
    data.GridRows = (from e in db.EmployeeSet
                     select new RosterGridRow() { Employee = e }).ToList();
    // Load Rosterentries for selected Month
    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();
    }
    return View(data);


In my Controller i get four error messages now, that the Names of "startMonth", "startYear", "startDate" and "endDate" aren't be in the current Context.

With the using .....Models.ViewModels; i get no solution.
 
Share this answer
 
Comments
Philippe Mori 9-Jul-11 13:05pm    
RosterIndexViewModel constructor should still uses 2 DateTime as your original code (ideally with my suggestion of using a tempoarary variable today and the clearer construction of a DateTime at the beginning of a month. The constructor will the copy those values as member of the model class (assuming that you have integrated my code into you model).

using in C# is for using types without specifying their full namespace. The other use if for automatic disposing of object.
Hello Phillippe,

at the moment i stay on the line. Please can you make me a suggestion please?

I have changed now following steps:

RosterIndexViewModel.ccs - File:
XML
namespace RosterManagementSystemWeb.Models.ViewModels
{
    public class RosterIndexViewModel
    {
        public RosterIndexViewModel(int year, int month)
        {
            DaysofMonth = new List<DateTime>();
            var d = new DateTime(year, month, 1);
            while (d.Month == month)
            {
                DaysofMonth.Add(d);
                d = d.AddDays(1);
            }
        }

        public class MonthListView
        {
            // MonthList-Model for Dropdownlist
            public int? SelectedMonth;
            public SelectList MonthList
            {
                get
                {
                    return new SelectList(AllMonths, "Index", "Name", SelectedMonth);
                }
            }
            public IEnumerable<MonthInfo> AllMonths
            {
                get
                {
                    CultureInfo info =
                        System.Threading.Thread.CurrentThread.CurrentCulture;
                    var today = DateTime.Today;
                    DateTime startDate = new DateTime(today.Year, today.Month, 1);
                    DateTime endDate = startDate.AddMonths(1).AddDays(-1);
                    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, Name = monthName };
                        }
                        ++index;
                    }
                }
            }
            public class MonthInfo
            {
                public int Index { get; set; }
                public string Name { get; set; }

            }
        }
    }
    public class RosterGridRow
    {
        public Employee Employee { get; set; }
        public IEnumerable<Roster> RosterEntries { get; set; }
        public RosterGridRow()
        {
            RosterEntries = new List<Roster>();
        }
    }
}


RosterController.cs - File:
namespace RosterManagementSystemWeb.Controllers
{

    public class RosterController : Controller
    {
        private RosterDb db = new RosterDb();

        //
        // GET: /Roster/

        public ActionResult Index()
        {
            
            
            
            //DateTime startdate = DateTime.Today.AddDays((DateTime.Today.Day - 1) * -1);
            //DateTime enddate = startdate.AddMonths(1).AddDays(-1);



            var data = new RosterIndexViewModel(2011, 8);

            //var data = new RosterIndexViewModel(startdate.Year, startdate.Month);
            data.GridRows = (from e in db.EmployeeSet
                             select new RosterGridRow() { Employee = e }).ToList();

            // Load Rosterentries for selected Month
            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();
            }

            return View(data);
        }


In the RosterController.cs on the line -> public RosterIndexViewModel(int year, int month), i'm not sure how can i get the right result.

I hope you can helo otherwise i must wait until to Thursday that help me my Trainer.

Thank you :)
 
Share this answer
 
While surfing i have found one link this may help you

http://www.dotnetpools.com/2012/10/binding-dropdownlist-in-mvc3-using-c.html[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900