Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 MVC DateTime
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 8-Jul-11 12:03pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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; }
    }
}
  Permalink  
v2
Comments
Marcus Kramer at 9-Jul-11 10:41am
   
Nicely done.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  
Comments
Philippe Mori at 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);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanks Smile | :)
 
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:
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.
  Permalink  
Comments
Philippe Mori at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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:
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 Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

While surfing i have found one link this may help you
 
http://www.dotnetpools.com/2012/10/binding-dropdownlist-in-mvc3-using-c.html[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100